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.

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?")

share|improve this question
    
This is very difficult, I'm afraid. –  karlkoeller Feb 16 at 7:49

2 Answers 2

up vote 7 down vote accepted

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

share|improve this answer
    
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 at 9:41

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

share|improve this answer
    
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 at 8:27

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.