Латекс + Импорт исходного кода - PullRequest
5 голосов
/ 22 апреля 2010

Я использую Latex для написания программы, в которой перечислен весь мой код, и я следую за этим:

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

Это работает, но мой код работает сбоку страницы,Как я могу это исправить?Дополнительный вопрос: как я могу выделить синтаксис?У меня есть lang установлен на Java.

Ответы [ 5 ]

9 голосов
/ 22 апреля 2010

Попробуйте что-то вроде этого:

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

, который производит:

альтернативный текст http://img260.imageshack.us/img260/1608/codes.png

2 голосов
/ 22 апреля 2010

Вместо того, чтобы включать источник напрямую, включите его из файла:

\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}
}
2 голосов
/ 22 апреля 2010

Вы хотите включить разрыв строки с помощью

\lstset{breaklines=true} 

в опциях команды. Теперь вам может не понравиться его выбор, но это другой вопрос.

2 голосов
/ 22 апреля 2010

Рассматривали ли вы

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

1 голос
/ 22 апреля 2010

Использование breaklines=true Например:

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

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...