中心乳胶lstlisting

Bru*_*noB 22 latex listings

这真让我抓狂.

我想把一个名单集中在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}
Run Code Online (Sandbox Code Playgroud)

这是将赌注放在中心,而不是它的标题,这是右边的.如果我取出表格,那么标题会居中,但代码会向左移动!:(

谢谢.

小智 25

您应该考虑使用xleftmarginxrightmargin(参见texdoc listings章节4.10),而不是使用线宽.以下代码在没有任何中心或小型环境的情况下工作:

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


Ben*_*ier 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}
Run Code Online (Sandbox Code Playgroud)

您没有解释为什么您希望分隔线很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}
Run Code Online (Sandbox Code Playgroud)

  • 使用`\ begin {center}`对我的列表中心没有任何影响. (4认同)

小智 5

实际上,对我有用的是相反的做法:将微型页面放在中心环境中。