[Tex/LaTex] Listings for Tikz code

listingstikz-pgf

I am trying to quote (not compile!) LaTeX code (including TikZ code) within a LaTeX document. The verbatim package works, but it doesn't wrap text and doesn't look very nice. So I am trying to use the package listings, but am having problems:

It is giving me issues on the quoted code when I try to compile, when it should just be treating it like text! I imagine this is because it is trying to color the syntax or something, but at this point that would be a bonus — I just want the code to look nice and wrap (and not compile within \begin{lstlisting}).

Also, it seems that the TikZ code that I use within a LaTeX document is also giving listings trouble.

\usepackage{listings}

\lstset{
language=TeX,                       
defaultdialect=empty,
basicstyle=\footnotesize,           
numbers=left,                           
numberstyle=\footnotesize,          
stepnumber=5,                           
numbersep=5pt,                      
backgroundcolor=\color{white},  
showspaces=false,                   
showstringspaces=false,             
showtabs=false,                     
frame=single,                   
tabsize=2,                  
captionpos=b,               
breaklines=true,                
breakatwhitespace=false,        
escapeinside={\%}{)}            
}


\begin{document}
The document begins.  A bunch of regular Latex code and TikZ pictures here.
\begin{lstlisting}

%Set domain, range, and scale of picture.
\begin{tikzpicture}[domain=0:100,range=0:200,scale=0.7,thick]
\usetikzlibrary{calc}

%Define linear parameters 
\def\inc{10}            %Total freight.
\def\pa{1}              %Price of x1.
\def\pb{1}              %Price of x2.
\def\panew{0.5}     %New price for x1.

% Define coordinates.
\coordinate (x2) at  (0,{\inc/\pb});
\coordinate (x1) at  ({\inc/\pa},0);
\coordinate (x1') at  ({\inc/\panew},0);

\end{lstlisting}
\end{document}

Note: I do not want the TikZ picture to render within \begin{lstlisting}, I just want to be able to show the code. Same with the the LaTeX code within it.

Is there a better way to tackle this?

Best Answer

You are explicitly requesting that everything from a % to the next ) be executed as LaTeX code, removing the line escapeinside={\%}{)} fixes this.

Related Question