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.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am writing a long report, and I need to include some code I wrote. I would like my code to appear colorized, as can be seen in some text editors, but not by using the verbatim package.

I need to colorize two distinct languages:

  • Bash language
  • (Java language)<= Already have it, thanks!

Thank you in advance for your help.

share|improve this question
up vote 9 down vote accepted

Not contradicting anything in JoG's answer:

\documentclass{article}
\usepackage{xcolor}
\usepackage{listings}
\lstset{basicstyle=\ttfamily,
  showstringspaces=false,
  commentstyle=\color{red},
  keywordstyle=\color{blue}
}

\begin{document}

\begin{lstlisting}[language=Java,caption={Java version}]
public class HelloWorld {
   // Here's the main class
   public static void main(String[] args) {
       System.out.println("Hello, world!");
   }
}
\end{lstlisting}

\begin{lstlisting}[language=bash,caption={bash version}]
#!/bin/bash
echo "Hello, world!"
\end{lstlisting}

\end{document}

enter image description here

share|improve this answer

for java i use the listings package with following style:

\definecolor{javakeyword}{rgb}{0,0,0.5}
\definecolor{javastring}{rgb}{0,0.5,0}
\definecolor{javacomment}{rgb}{0.5,0.5,0.5}
\lstdefinestyle{java}{
   language=Java,
   showspaces=false,
   showstringspaces=false,
   basicstyle=\ttfamily,
   columns=flexible,
   stringstyle=\color{javastring},
   keywordstyle=\color{javakeyword}\ttfamily\textbf,
   commentstyle=\color{javacomment}\ttfamily\textit
 }

which i then use like

\begin{lstlisting}[float,style=Java,caption={Correct Logging example},label=lst:logging]
  private static final Log log = LogFactory.getLog(MyClass.class);

  public void doSomeStuff(Stuff stuff) throws StuffException {
      checkNotNull(stuff,"stuff should not be null");
  }
\end{listlisting}
share|improve this answer
1  
Welcome to TeX.sx! Can you turn your code snippets into a complete minimal working example (MWE)? Again, welcome and we look forward for more of your contributions to the TeX.sx. – hpesoj626 Jan 4 '13 at 11:46
    
listings also supports bash and several other languages. – marczellm Jan 4 '13 at 12:04
    
@marczellm Indeed, the OP should use sh as the language because Java isn't appropriate and it's the best alternative to using bash. – Marc van Dongen Jan 4 '13 at 13:16
    
@MarcvanDongen Ermmm... what? – marczellm Jan 4 '13 at 13:24
    
@JoG:Thanks for Java it's working well, but what about bash and some special chars (like $) etc. – user23710 Jan 4 '13 at 13:40

listings does not work well for shell code because it can not deal well with a lot of codes such as the following one:

\documentclass{article}
\usepackage{xcolor}
\usepackage{listings}
\lstset{basicstyle=\ttfamily,
  showstringspaces=false,
  commentstyle=\color{red},
  keywordstyle=\color{blue}
}
\begin{document}
\begin{lstlisting}[language=bash,caption={bash version}]
#!/bin/bash
echo the $# parameter destroys listings syntax highlighting
\end{lstlisting}
\end{document}

enter image description here

It does not produce what you could expect (the # sign is considered as a comment delimiter and thus the rest of the line is not colored as it should.

For bash you may instead use the minted package which need an exterior tool to produce the highlighting (the pygments python library).

Compare the previous result with the one produced by this code (you need to call the compiler with pdflatex -shell-escape myfile.tex):

\documentclass{article}

\usepackage{minted}

\begin{document}

\begin{minted}{bash}
#!/bin/bash
echo the $# parameter did not destroy pygments syntax highlighting
\end{minted}

\end{document}

enter image description here

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.