Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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.
share|improve this question
    
So to say this a bit more concisely, you'd like to do some text-processing on the Java file itself tex-time so that 1) the Java source itself isn't altered [permanently?] and 2) the comments line up in the output. –  Sean Allred Dec 20 '12 at 18:04
    
Before it is in TeX it is formatted fine. Like the comments in my Java file are neatly lined up. Often they end up jumbled in TeX. I am mostly concerned about the comments lining up. –  CodeKingPlusPlus Dec 20 '12 at 18:10
    
Being OCD myself (but not really - that'd suck) I completely understand. –  Sean Allred Dec 20 '12 at 18:12
1  
How are you aligning your comments? Are you using mixed tabs and spaces, or only spaces? Does the same thing happen if you paste the code inside a lstlisting environment, or only when the code is brought in via \lstinputlisting? Can you edit your question to include a few of the lines of code that cause trouble? –  G. Poore Dec 23 '12 at 4:17
    
I think we need an example of your Java code here so we can work out exactly what is up. –  Joseph Wright Jan 5 '13 at 21:24

2 Answers 2

up vote 1 down vote accepted

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.

share|improve this answer

This is not what you're looking for, but to be perfectly precise, you can alter you code inside TeX explicitly, with the lstlisting environment:

\begin{lstlisting}[language=python]
for index in range(314):
    print "Easy as sweet potato pie."
\end{lstlisting}

This has its drawbacks, since you can't edit your Java-source to correct actual bugs without having to go back and make the same changes to your TeX, fixing the comments.

On the upside, you have direct (yet limited) control over the output. The comments may not line up perfectly, in which case it may yet be possible to do one of two things:

  1. Change the font listings uses by default.
  2. Do some relatively serious text processing. With \@for and crap. And other stuff. (Or perhaps LuaTeX? I haven't messed with it...)

See also http://tex.stackexchange.com/a/31352/17423 for a good solution if you replicate the source in TeX. Unfortunately, this solution requires you to put a hard limit on the width of your comments. It is possible to define multiple spacings, though.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.