2010-10-20 63 views
4

我想在乳膠中製作雙列代碼,但在兩列上方有標題。例如:配置使用多色(乳膠)列表環境的標題

Program 1 : some text here for the caption 

1 //col 1    7 //col2 
2 void main (void)  8 void foo(void) 
3 {     9 { 
4  //just a test 10  int c; 
5  return 0;  11  c = 0; 
6 }     12 } 

我使用上市。爲了完成上述佈局,我在一張桌子內切出了兩個列表。這種方法存在一些問題,我必須手動分割代碼並觀察PDF中的結果。

我想使用[multicol = 2]選項,但是隻有第一列的標題棧。有誰現在如何克服(如果可能的話?)

Program 1 : some text 7 //col2 
    here for the caption 8 void foo(void) 
1 //col 1    9 { 
2 void main (void)  10  int c; 
3 {     11  c = 0; 
4  //just a test  12 } 
5  return 0;   
6 }      

正下方是不工作的代碼

\documentclass{article} 
\usepackage{listings} 
\usepackage{multicol} 
\begin{document} 

\lstset{ 
    language=C, 
    breaklines=true, 
    prebreak = \raisebox{0ex}[0ex][0ex]{\ensuremath{\hookleftarrow}}, 
} 

\begin{lstlisting}[stepnumber=1,multicols=2,caption=Caption test to confirm that multicol does not like large captions] 
void MudaDigito(char val) 
{ 
    if (pos == 0) 
    { 
    v0 = val; 
    } 
    if (pos == 1) 
    { 
    v1 = val; 
    } 
    if (pos == 2) 
    { 
    v2 = val; 
    } 
    if (pos == 3) 
    { 
    v3 = val; 
    } 
} 
\end{lstlisting} 

\ {結束}文件

+1

想,如果你可以只添加標題(可能是部分或只是大膽的文字),你顯示你的上市地位之前,它的工作?所以它將獨立於列表包。 – nbz 2010-10-21 11:25:29

+0

@nEm:很好的建議,在某些情況下還有一個選項,但是你不能使用LaTeX的字幕編號工具。我還試驗過將中間部分和標題都包括在內,因爲標題保持2列格式,所以結果看起來不太明顯。 – 2012-10-06 10:58:28

回答

2

我有同樣的問題。我找到了解決方案。 它不乾淨,但它的工作原理。 下面是工作代碼。


\documentclass{article} 
\usepackage{listings} 
\usepackage{multicol} 
\newcommand{\listingcaption}[1]% 
{% 
\refstepcounter{lstlisting}\hfill% 
Listing \thelstlisting -- #1\hfill%\hfill% 
}% 
\begin{document} 
\lstset{ 
    language=C, 
    breaklines=true, 
    prebreak = \raisebox{0ex}[0ex][0ex]{\ensuremath{\hookleftarrow}}, 
} 
\listingcaption{Caption test to confirm that multicol does not like large captions\label{list1}} 
\begin{lstlisting}[stepnumber=1,multicols=2] 
void MudaDigito(char val) 
{ 
    if (pos == 0) 
    { 
    v0 = val; 
    } 
    if (pos == 1) 
    { 
    v1 = val; 
    } 
    if (pos == 2) 
    { 
    v2 = val; 
    } 
    if (pos == 3) 
    { 
    v3 = val; 
    } 
} 
\end{lstlisting} 
See listing \ref{list1} 
\end{document} 
+0

注意:生成的字幕不會出現在列表中。 (至少不使用分類) – 2012-10-06 10:55:36

+1

不適用於我。標題顯示在文本中,而不是列表本身。我希望列表顯示爲float = t。 – Mahdi 2013-03-31 14:43:18