2010-06-24 98 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