2010-06-24 101 views
20

这让我疯狂。Centre Latex lstlisting

我想在LaTeX中集中一个列表。

3小时后尝试这里的一些代码:

\lstset{ % 
    caption=Descriptive Caption Text, 
    label=lst:descr_capti_text, 
    basicstyle=\ttfamily\footnotesize\bfseries, 
    frame=tb, 
    linewidth=0.6\textwidth 
} 
\centering\begin{tabular}{c} 
\begin{lstlisting} 
printf("this should be centered!"); 
\end{lstlisting} 
\end{tabular} 

这是把在中心lstlisting但不是它的标题,即转到正确的。如果我拿出表格,那么标题会居中,但代码会在左边! :(

谢谢。

+4

只是想我会提[乳胶StackExchange建议(http://area51.stackexchange.com/proposals/2148/tex-latex-and-朋友?referrer = 5z6F4dAmVScN5QfMOt5KEg2),因为一旦起飞,它将成为这类问题的最佳场所。 – 2010-06-24 01:25:29

回答

7

您的字幕,而其中心结束了,你只是做沿着顶部和上市仅0.6\textwidth长期的底部运行线上市,这使得它看起来好像字幕是偏离中心的同时,您的\centering不居中上市(可见如果不缩短低于线及以上)

这应该工作:。

\begin{center} 
    \lstset{% 
    caption=Descriptive Caption Text, 
    basicstyle=\ttfamily\footnotesize\bfseries, 
    frame=tb 
    } 
    \begin{lstlisting} 
    printf("this should be centered!"); 
    \end{lstlisting} 
\end{center} 

你不解释为什么你想要delimiti ng行长度为0.6\textwidth。如果 您实际上想要将您的列表宽度设置为该值,那么您的 方法就无法达到您想要的效果。使用类似于小型文件夹的方式为整个列表设置宽度 。

begin{minipage}{0.6\textwidth} 
    \begin{center} 
    \lstset{% 
     caption=Descriptive Caption Text, 
     basicstyle=\ttfamily\footnotesize\bfseries, 
     frame=tb, 
    } 
    \begin{lstlisting} 
     printf("this should be centered!"); 
    \end{lstlisting} 
    \end{center} 
\end{minipage} 
+0

虽然和Akim一样,但我发现它与中心环境中的小型设备一起工作。 – luketorjussen 2012-04-07 10:58:42

+1

不幸的是,这并不适合我。该列表不会移动。 – kaoD 2013-06-05 02:41:56

+3

使用'\ begin {center}'对我来说没有任何影响。 – 2013-11-05 21:15:52

4

实际上,对我来说工作的是反向:将小型设备放在中心环境中。

+0

同样的事情对我来说也是如此 – Cemre 2012-05-31 23:45:54

19

除了使用线宽,你应该考虑使用xleftmarginxrightmargin(参见texdoc listings,第4.10节)的。下面的代码工作没有任何中心或minipage环境:

\lstset{ 
    caption=Descriptive Caption Text, 
    basicstyle=\footnotesize, frame=tb, 
    xleftmargin=.2\textwidth, xrightmargin=.2\textwidth 
} 
\begin{lstlisting} 
    printf("this should be centered!"); 
\end{lstlisting} 
+0

令人惊叹的是,这正是我一直在寻找的。在我的情况下,我在'xrightmargin'上使用了一个负值,所以我可以移动lslisting帧... 感谢您的提示! – guipy 2015-08-08 18:43:17