【问题标题】:Long lines in LaTeXLaTeX 中的长线
【发布时间】:2010-10-05 09:18:29
【问题描述】:

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

谢谢, 卢锡安

【问题讨论】:

    标签: latex long-lines


    【解决方案1】:

    当然,您有很棒的“列表”包。它甚至支持(如果您愿意)许多源代码语言的漂亮打印,以及根据所显示的语言“智能中断”长行。

    【讨论】:

      【解决方案2】:

      你可以只使用固定宽度的字体吗,例如\texttt{}?如果您需要更复杂的东西,请尝试使用 moreverblistingsalgorithmicx 软件包。

      【讨论】:

        【解决方案3】:

        我强烈建议为此使用 Listings 包。它将很好地格式化代码,并支持大量的语言(参见Pg12),并在三个方面有效地使用:

        1. 使用 \lstinline!int x = 0;! 命令包含内联 sn-ps
        2. 类型化代码块,在lstlisting 环境中,如下所示

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

        这个包可以配置很多很多选项,例如,换行和包含行号,完整的详细信息可以在package documentation 中找到,但是@中提供了一个很好的基本设置987654323@:

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

        【讨论】:

          【解决方案4】:

          你可以试试像 LGrind 这样的漂亮打印包。否则你将不得不手动 打破界限。

          【讨论】:

            【解决方案5】:

            您可能还对fancyvrb package 感兴趣。

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多