【问题标题】:Latex + Source Code ImportLatex + 源代码导入
【发布时间】:2011-02-11 01:28:27
【问题描述】:

我正在使用 Latex 编写一个列出我所有代码的程序,我正在关注这个:

http://texblog.wordpress.com/2008/04/02/include-source-code-in-latex-with-listings/

它有效,但我的代码运行在页面的一侧。我怎样才能解决这个问题? 附加问题:如何让它突出显示语法?我确实将 lang 设置为 Java。

【问题讨论】:

    标签: latex


    【解决方案1】:

    试试这样的:

    \documentclass{article}
    
    \usepackage{listings}
    \usepackage{color}
    \usepackage{textcomp}
    \definecolor{listinggray}{gray}{0.9}
    \definecolor{lbcolor}{rgb}{0.9,0.9,0.9}
    \lstset{
        language=c,
        basicstyle=\scriptsize,
        upquote=true,
        aboveskip={1.5\baselineskip},
        columns=fullflexible,
        showstringspaces=false,
        extendedchars=true,
        breaklines=true,
        showtabs=false,
        showspaces=false,
        showstringspaces=false,
        identifierstyle=\ttfamily,
        keywordstyle=\color[rgb]{0,0,1},
        commentstyle=\color[rgb]{0.133,0.545,0.133},
        stringstyle=\color[rgb]{0.627,0.126,0.941},
    }
    
    \begin{document}
    
    \begin{lstlisting}
    
    #include <stdio.h>
    
    int main() 
    {
        // A line comment
        printf("A really, really, really, really, really, really, really, really, really, really, really, really, really, really, really, really, REALLY long line. && & \n");
        return 0;
    }
    
    \end{lstlisting}
    
    \end{document}
    

    产生:

    alt text http://img260.imageshack.us/img260/1608/codes.png

    【讨论】:

    • 如何让乳胶自动转义 & 和 \n?
    • 美学注释:columns=fixed 通常会产生不好的结果(就像这里,字母间隔太多)。我更喜欢 columns=fullflexible,即使是固定宽度的字体,它也能让代码看起来更自然。
    • @Damien:同意,看起来更好!编辑了我的答案。
    【解决方案2】:

    你考虑过

    \lstset{...}
    breaklines=true -> sets automatic line breaking
    breakatwhitespace=false -> automatic breaks happen at whitespace
    

    ?

    【讨论】:

      【解决方案3】:

      你想用

      开启换行
      \lstset{breaklines=true} 
      

      在命令选项中。现在你可能不喜欢它的选择,但这是另一个问题。

      【讨论】:

        【解决方案4】:

        不要直接包含源代码,而是从文件中包含它:

        \lstinputlisting{/Volumes/docs/p2k_files_in_qt.sh}
        

        这样做可以节省大量工作。或者,如果您必须在文件中包含源代码,则可以使用 lgrind

        \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=1,                   % 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{Blue},  % 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
        % size, font
        commentstyle=\fontsize{7}{7}\selectfont,
        basicstyle=\ttfamily\fontsize{7}{7}\selectfont,
        keywordstyle=\color{red},
        commentstyle=\color{blue},
        stringstyle=\color{green}
        }
        

        【讨论】:

          【解决方案5】:

          使用breaklines=true e。 g.:

          \lstnewenvironment{bash}
          {\lstset{language=bash,breaklines=true,frame=trBL}}
          {}

          【讨论】:

            猜你喜欢
            • 2011-05-25
            • 2010-12-30
            • 1970-01-01
            • 2017-01-11
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2013-04-08
            相关资源
            最近更新 更多