[Tex/LaTex] Formatting the Listing

formattinglistings

The listing I have in my paper is Java Code. When it is displayed through TeX I lose some of the formatting that I did inside my .java files. For example, all of my comments in my code were lined up, but now after incorporating the Java code in TeX the comments are no longer lined up. Here is a visual of this dilemna:

enter image description here

Notice how all the comments are lined up except the first few at the top. Clearly there is room to the right for all of the comments to be shifted.

Here is my TeX code for the listing:

\section{Code}


\lstset{language=Java, basicstyle=\tiny\ttfamily,
    linewidth=\textwidth,
    numbers=left,
    columns=flexible,
    numbersep=5pt,      % Abstand der Nummern zum Text
    tabsize=3,
    breaklines=true,
    frame=bottomline,
    showspaces=false,
    showtabs=false,
    xleftmargin=17pt,
    breakatwhitespace=true,
    framexleftmargin=17pt,
    framexrightmargin=5pt,
    framexbottommargin=4pt,
    showstringspaces=false,
    literate=%
        {Ö}{{\"O}}1
        {Ä}{{\"A}}1
        {Ü}{{\"U}}1
        {ß}{{\ss}}2
        {ü}{{\"u}}1
        {ä}{{\"a}}1
        {ö}{{\"o}}1
        }
\lstinputlisting{EncodingScheme.JAVA}
  1. Are there ways to edit my code inside TeX manually?
  2. How can I get page-breaks at certain points in the code? For example, if I wanted to see the for-loop that is at the bottom of the page in the picture what would I do?
  3. Please give all related suggestions to formatting code inside TeX.

Best Answer

If your comments are aligned in the source code, but not in a lstlisting environment, the reason usually is that in the source code there are TABs and spaces and that the TAB width of your source code editor is not in line with the one used by listings. You can change the latter with the tabsize=<number> option.

Nevertheless it is also possible to automatically align all comments. In the following, I overwrite the // delimiter with morecomment to "format" the comment with a \lstCom[<indent length>] command. The \lstCom command uses the tabto package to typeset all comments a fixed position from the left, resulting in perfectly alligned comments no matter what the source code looks like:

\documentclass[DIV=15]{scrartcl}
\usepackage[T1]{fontenc}
\usepackage{listings,beramono,helvet}
\usepackage{tabto}

\makeatletter
\newenvironment{lstComment}[1][]
{\begingroup\def\lst@com@width{#1}\begin{lrbox}{\@tempboxa}}
{\end{lrbox}\lst@com@box[\lst@com@width]{\@tempboxa}\endgroup}

\newcommand\lstCom[1][11cm]{%
  \begin{lstComment}[#1]\bgroup\aftergroup\lst@com@endenv%
}
\def\lst@com@endenv{%
  \end{lstComment}%   
  \egroup
}
\newcommand{\lst@com@box}[2][]{%
  \tabto{#1}\usebox{#2}%
}
\makeatother

\lstset{
  language={Java},basicstyle=\ttfamily,columns=fullflexible
}

\begin{document}

\lstset{ morecomment=**[l][{\lstCom[10cm]}]{//} }
\begin{lstlisting}
public class HelloWorld { // We may have  many classes
  public static void main(String[] args) { // but only one main() method!
    System.out.println("Hello, World");
  } // here we terminate
} // end of the world
\end{lstlisting}

\end{document}

enter image description here

You have to play a bit around with the parameter given to \lstCom in the morecomment= line to get a suitable indentation. If a source line is larger than the indent, the comment will be inserted as an extra line. However, comments itself are not broken across lines if they exceed the available space. This should be possible, but would require some extra work.

Related Question