乳胶+源代码导入

发布于 2024-08-29 16:18:22 字数 336 浏览 5 评论 0原文

我正在使用 Latex 编写一个程序,列出我的所有代码,我遵循以下步骤:

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

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

I'm using Latex to write a program listing all my code and I am following this:

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

It works, but my code runs of the side of the page. How can I fix this?
Additional question: How can I get it to highlight syntax? I do have lang set to Java.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

如此安好 2024-09-05 16:18:22

尝试这样的操作:

\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 文本 http://img260.imageshack.us/img260/1608/codes。 .png

Try something like this:

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

which produces:

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

夜血缘 2024-09-05 16:18:22

你考虑过吗

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

Did you consider

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

?

迷荒 2024-09-05 16:18:22

打开换行符

\lstset{breaklines=true} 

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

You want to turn on line breaking with

\lstset{breaklines=true} 

in the command options. Now you might no like its choices but that is another question.

山田美奈子 2024-09-05 16:18:22

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

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

Instead of including the source directly, include it from a file:

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

You'll save lots of work doing it that way. Alternatively, if you have to include source into the file, there's 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}
}
木緿 2024-09-05 16:18:22

使用 breaklines=true 例如:

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

Use breaklines=true e. g.:

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文