【问题标题】:Configure caption on listings environment using multicol (latex)使用 multicol (latex) 在列表环境中配置标题
【发布时间】:2010-10-20 20:06:33
【问题描述】:

我想在乳胶中制作一个双列代码,但标题在两列上方。例如:

  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}

\end{文档}

【问题讨论】:

  • 如果您可以在显示您的列表之前添加一个标题(可能是一个部分或只是粗体文本),它会起作用吗?所以它将独立于 Listings 包。
  • @nEm:很好的建议,并且在某些情况下是一个选项,尽管那样你就不能使用 LaTeX 的标题编号功能。我还尝试同时包含居中部分和标题,由于标题保持为 2 列格式,结果在视觉上并不令人印象深刻。

标签: latex listings


【解决方案1】:

我有同样的问题。我找到了解决方案。 它不干净,但它有效。 下面是工作代码。


\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}

【讨论】:

  • 注意:生成的标题不会出现在列表中。 (至少如果使用经典论文则不会)
  • 对我不起作用。标题显示在文本中,而不是列表本身。我希望列表显示为 float=t。
猜你喜欢
  • 1970-01-01
  • 2018-10-15
  • 1970-01-01
  • 2017-06-27
  • 2011-03-17
  • 1970-01-01
  • 1970-01-01
  • 2021-09-07
  • 2014-09-23
相关资源
最近更新 更多