6

My goal is to have a somewhat large verbatim block and use listings to give syntax highlighting to only particular parts. Here's a toy example:

\begin{verbatim}
How to include files in:
 C:      #include <stdlib.h>
 C++:    #include <iostream>
 python: import math, from math import exp
 Java:   import java.util.arraylist;
\end{verbatim}

which should become

\begin{lstlistings}[myoptions]
How to include files in:
 C:      \lstuselanguage{c}{#include <stdlib.h>}
 C++:    \lstuselanguage{c++}{#include <iostream>}
 python: \lstuselanguage{python}{import math, from math import exp}
 Java:   \lstuselanguage{java}{import java.util.arraylist;}
\end{lstlistings}

where \lstuselanguage is some sort of command that does syntax highlighting in the specified language.

Is this possible? (In LaTeX, all things are possible, so I guess what I mean is: "how hard is this?")

1
  • This is very difficult, I'm afraid. Feb 16, 2014 at 7:49

2 Answers 2

7

Unfortunately lstlisting environments can not be embedded inside other environments.

Here I define a new environment lstuselanguage which takes an argument (the language):

\newenvironment{lstuselanguage}[1]{%
  \noindent%
  \begin{minipage}{0.15\linewidth}%
  \texttt{#1:}%
  \end{minipage}%
  \begin{minipage}{0.85\linewidth}%
}{%
\end{minipage}%
}

to be used in this way:

\documentclass{article}
\usepackage{listings}
\lstnewenvironment{C}
  {\lstset{language=C}}
  {}
\lstnewenvironment{CPP}
  {\lstset{language=C++}}
  {}
\lstnewenvironment{Java}
  {\lstset{language=Java}}
  {}
\lstnewenvironment{Python}
  {\lstset{language=Python}}
  {}

\newenvironment{lstuselanguage}[1]{%
  \noindent%
  \begin{minipage}{0.15\linewidth}%
  \texttt{#1:}%
  \end{minipage}%
  \begin{minipage}{0.85\linewidth}%
}{%
\end{minipage}%
}


\begin{document}
\noindent How to include files in:

\begin{lstuselanguage}{C}
\begin{C}
#include <stdlib.h>
\end{C}
\end{lstuselanguage}

\begin{lstuselanguage}{C++}
\begin{CPP}
#include <iostream>
\end{CPP}
\end{lstuselanguage}

\begin{lstuselanguage}{Python}
\begin{Python}
import math, from math import exp
\end{Python}
\end{lstuselanguage}

\begin{lstuselanguage}{Java}
\begin{Java}
import java.util.arraylist;
\end{Java}
\end{lstuselanguage}

\end{document} 

Output:

enter image description here

1
  • Thanks! I guess a key trick to keep in mind is to use minipage like this to separate the code from the non-code.
    – usul
    Feb 16, 2014 at 9:41
2

I think this should help you out. Define environments and use them as required. Here is the code as requirement:

\documentclass{article}
\usepackage{listings}
\lstnewenvironment{C}
  {\lstset{language=C}} 
%Add your addition parameters as required like showstringspaces , line numbering , 
% frames , etc.seperated by a comma as shown in the CPP  environment 
  {}
\lstnewenvironment{CPP}
  {\lstset{language=C++,basicstyle=\ttfamily\small,frame=none}}
  {}
\lstnewenvironment{Java}
  {\lstset{language=Java}}
  {}
\lstnewenvironment{Python}
  {\lstset{language=Python}}
  {}

\begin{document}
How to include files in:\\
C:
\begin{C}
#include <stdlib.h>
\end{C}
C++:
\begin{CPP}
#include <iostream>
\end{CPP}
Python:
\begin{Python}
import math, from math import exp
\end{Python}
Java:
\begin{Java}
import java.util.arraylist;
\end{Java}
\end{document}

enter image description here

1
  • Ideally, it would be great to keep have regular text and coded text on the same line and all within a code environment, but this is definitely better than nothing and may end up being my best option. Thanks!
    – usul
    Feb 16, 2014 at 8:27

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .