我有一些在逐字环境中编写的代码。有时,行太长,生成的 pdf 看起来很难看 - 文本行超出了右边距。我该怎么做才能自动打破这些长线?还有什么比逐字环境更好的吗?

谢谢,露西安

有帮助吗?

解决方案

您可以只使用固定宽度的字体吗? \texttt{}?如果您需要更复杂的东西,请尝试 更动词, 列表, , 或者 算法x 包。

其他提示

当然,你有那美妙的“清单”包。它支持(如果你想)很多源代码语言的就算不错的打印,以及取决于语言排长队“智能断”的显示。

我强烈建议为此使用列表包。它将很好地格式化代码,并支持大量语言(请参阅 第12页),并通过三种方式有效使用:

  1. 内联片段包含使用 \lstinline!int x = 0;! 命令
  2. 键入的代码块,在 lstlisting 环境,比如下面的

    \begin{lstlisting}
    for i:=maxint to 0 do
    begin
    { do nothing }
    end;
    \end{lstlisting}
    
  3. 导入的代码块,其中代码直接从源代码文件嵌入到文档中,例如使用: \lstinputlisting{source.py}

有很多很多选项可以使用此包进行配置,例如换行并包含行号,完整的详细信息可以在 包文档, ,但提供了一个很好的基本设置 维基百科条目:

\lstset{ %
language=C,                     % choose the language of the code
basicstyle=\footnotesize,       % the size of the fonts that are used for the code
numbers=left,                   % where to put the line-numbers
numberstyle=\footnotesize,      % the size of the fonts that are used for the line-numbers
stepnumber=2,                   % the step between two line-numbers. If it's 1 each line will be numbered
numbersep=5pt,                  % how far the line-numbers are from the code
backgroundcolor=\color{white},  % choose the background color. You must add \usepackage{color}
showspaces=false,               % show spaces adding particular underscores
showstringspaces=false,         % underline spaces within strings
showtabs=false,                 % show tabs within strings adding particular underscores
frame=single,                  % adds a frame around the code
tabsize=2,                    % sets default tabsize to 2 spaces
captionpos=b,                   % sets the caption-position to bottom
breaklines=true,                % sets automatic line breaking
breakatwhitespace=false,        % sets if automatic breaks should only happen at whitespace
escapeinside={\%*}{*)}          % if you want to add a comment within your code
}

您可以尝试一个漂亮的印刷包装一样LGrind。否则,你就必须手动来 打破线。

您可能也有兴趣在 fancyvrb包

scroll top