对于一个快速参考指南,我想在其中包含许多小列表,每个列表都有一个标题,如何将它们组合在一起,并防止代码被分解?我确信有一些窗口/孤立控件,但有一个问题是,如果代码太长,那么就没有办法不分解它,所以理想情况下应该有一些逻辑来防止代码分解(并且肯定要将标题与代码一起保留),同时如果代码太大则允许它。
以下代码基于 Christian Hupfer 建议的解决方案。外观很漂亮,但它显示标题 Listing0 和 listing1,并且在建议的解决方案中(副标题?)显示在顶部,而在我的解决方案中它显示在底部。我想删除副标题并将标题设置为所需的文本,但我不知道如何做到这一点。
它看起来是这样的:
\documentclass[12pt]{article}
\usepackage{listings}
\usepackage[letterpaper, portrait, margin=0.8in]{geometry}
\usepackage[usenames,dvipsnames]{color}
\title{\bf Test Title }
\definecolor{lgray}{rgb}{0.9,0.9,0.9}
\usepackage[most]{tcolorbox}
\lstset{
backgroundcolor=\color{lgray},
% basicstyle=\footnotesize \ttfamily \color{black} \bfseries,
breakatwhitespace=false,
breaklines=true,
captionpos=b,
commentstyle=\color{dkgreen},
deletekeywords={...},
escapeinside={\%*}{*)},
frame=single,
keywordstyle=\textbf,
morekeywords={BRIEFDescriptorConfig,string,TiXmlNode,DetectorDescriptorConfigContainer,istringstream,cerr,exit},
identifierstyle=\color{black},
stringstyle=\color{blue},
language=Java,
numbers=right,
numbersep=5pt,
numberstyle=\tiny\color{black},
rulecolor=\color{black},
showspaces=false,
showstringspaces=false,
showtabs=false,
stepnumber=1,
tabsize=5,
title=\lstname,
}
\newtcblisting{mycode}[2][]{%
listing options={language={Java}},
listing only,listing remove caption=false,
breakable,
colback=yellow!20!white,
coltitle=black,
colbacktitle=white!60!black,
title={\lstlistingname~#2},
title after break={\raggedleft\lstlistingname\ \thelstlisting~ -- continued},
arc = 0pt, auto outer arc, #1
}
\newcommand{\java}{\begin{lstlisting}[language=Java]}
\usepackage{multicol}
\begin{document}
\begin{multicols}{2}
\begin{tcblisting}{listing options={language={Java},caption={Draws one red triangle}},
listing only,listing remove caption=false,
breakable,
colback=yellow!20!white,
coltitle=black,
colbacktitle=white!60!brown,
title={\lstlistingname\ \thelstlisting},
title after break={\raggedleft\lstlistingname\ \thelstlisting~ -- continued},
arc = 0pt, auto outer arc
}
void setup() {
size(600, 400, OPENGL);
}
void draw() {
background(0);
translate(width/2, height/2);
fill(255,0,0);
beginShape();
vertex(-200,200);
vertex(200,200);
vertex(0, -200);
endShape();
}
\end{tcblisting}
%\lstinputlisting[language=java,caption=A third java listing]{openglprocessing/test.java}
\end{multicols}
\end{document}
答案1
我认为,最简单的方法是使用tcolorbox
及其listings
库,其中包含\usepackage[most]{tcolorbox}
类似于它所listings
启用的包\newtcblistings
——可大量配置的用户定义的列表环境。
我为此展示了两个例子——直接使用\begin{tcblisting}
和\newtcblisting
。
重要的特征是breakable
选项。
有关详细描述,特别是样式和可能性,tcbinputlisting
我参考了tcolorbox 手册,当前版本 3.60 的第 13 章
\documentclass{article}
\usepackage[most]{tcolorbox}
\newtcblisting{mycode}[2][]{%
listing options={language={C}},
listing only,listing remove caption=false,
breakable,
colback=yellow!20!white,
coltitle=black,
colbacktitle=white!60!black,
title={\lstlistingname~#2},
title after break={\raggedleft\lstlistingname\ \thelstlisting~ -- continued},
arc = 0pt, auto outer arc, #1
}
\begin{document}
\begin{tcblisting}{listing options={language={C},caption={The sum of integers}},
listing only,listing remove caption=false,
breakable,
colback=yellow!20!white,
coltitle=black,
colbacktitle=white!60!brown,
title={\lstlistingname\ \thelstlisting},
title after break={\raggedleft\lstlistingname\ \thelstlisting~ -- continued},
arc = 0pt, auto outer arc
}
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char **argv)
{
long int k = 0;
long int sum = 0;
for ( k = 1 ; k <= 100 ; k++ )
{
sum += k;
printf("Current sum: %10d\n");
}
sum = 0;
/* Now the sum of squares */
for ( k = 1 ; k <= 10 ; k++ )
{
sum += (long int) pow(k,2);
printf("Current sum: %10d\n");
}
sum = 0;
/* Now the sum of cubes */
for ( k = 1 ; k <= 10 ; k++ )
{
sum += (long int) pow(k,3);
printf("Current sum: %10d\n");
}
/* And repeating the stuff to make the whole
stuff longer than necessary */
sum = 0;
for ( k = 1 ; k <= 100 ; k++ )
{
sum += k;
printf("Current sum: %10d\n");
}
sum = 0;
/* Now the sum of squares */
for ( k = 1 ; k <= 10 ; k++ )
{
sum += (long int) pow(k,2);
printf("Current sum: %10d\n");
}
sum = 0;
/* Now the sum of cubes */
for ( k = 1 ; k <= 10 ; k++ )
{
sum += (long int) pow(k,3);
printf("Current sum: %10d\n");
}
return(EXIT_SUCCESS);
}
\end{tcblisting}
\begin{mycode}[listing options={caption={Just a repeat}}]{Once again}
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char **argv)
{
long int k = 0;
long int sum = 0;
for ( k = 1 ; k <= 100 ; k++ )
{
sum += k;
printf("Current sum: %10d\n");
}
sum = 0;
/* Now the sum of squares */
for ( k = 1 ; k <= 10 ; k++ )
{
sum += (long int) pow(k,2);
printf("Current sum: %10d\n");
}
sum = 0;
/* Now the sum of cubes */
for ( k = 1 ; k <= 10 ; k++ )
{
sum += (long int) pow(k,3);
printf("Current sum: %10d\n");
}
/* And repeating the stuff to make the whole
stuff longer than necessary */
sum = 0;
for ( k = 1 ; k <= 100 ; k++ )
{
sum += k;
printf("Current sum: %10d\n");
}
sum = 0;
/* Now the sum of squares */
for ( k = 1 ; k <= 10 ; k++ )
{
sum += (long int) pow(k,2);
printf("Current sum: %10d\n");
}
sum = 0;
/* Now the sum of cubes */
for ( k = 1 ; k <= 10 ; k++ )
{
sum += (long int) pow(k,3);
printf("Current sum: %10d\n");
}
return(EXIT_SUCCESS);
}
\end{mycode}
\end{document}