3

I'm trying to escape the dollar sign within listings for TeX language but it is not working. I have tried this post without any success.

This is the MWE:

\documentclass[border={10pt 10pt 10pt 10pt}]{standalone}

\usepackage[dvipsnames]{xcolor}

\usepackage{listings}

% Python style for highlighting
\newcommand\latexstyle{\lstset{
  language=TeX,
  basicstyle=\scriptsize\ttfamily,
  mathescape=true,
  keywordstyle=\color{MidnightBlue}\bfseries,
  morekeywords={begin, addplot, addlegendentry, documentclass, usepackage, usetikzlibrary, usepgfplotslibrary, pgfplotsset, nextlist, coordinate small, draw},
}}

% Python environment
\lstnewenvironment{latex}[1][mathescape]
{
\latexstyle
\lstset{#1}
}
{}

\begin{document}

\begin{latex}
 \begin{tikzpicture}
  \begin{loglogaxis}

\addplot+ [ultra thick, dotted] table[]{
  10  0.2
  100  0.1
  1000  0.05}
coordinate [pos=0.15] (A)
coordinate [pos=0.25] (B);
\addlegendentry{legend 1}
\coordinate (A') at ($(A)!3pt!90:(B)$);
\coordinate (B') at ($(B)!3pt!270:(A)$);
\draw (A')-|(B') node[pos=0.25,above]{$1$}
  node[pos=0.75,right]{$\alpha$};
\draw [shorten <=-3pt,shorten >=-3pt] (A') -- (B');

  \end{loglogaxis}
\end{tikzpicture}
\end{latex}

\end{document}
aaragon
  • 3,041

1 Answers1

4

You're setting mathescape=true twice. You're possibly misunderstanding its meaning: with mathescape=true, listings will act as if a TeX math formula is meant when you type something like $\alpha$ (so printing a math alpha). I don't think you want it when typesetting a listing like this.

\documentclass[border={10pt 10pt 10pt 10pt}]{standalone}

\usepackage[dvipsnames]{xcolor}

\usepackage{listings}

% Python style for highlighting
\lstdefinestyle{latexstyle}{
  language=TeX,
  basicstyle=\scriptsize\ttfamily,
%  mathescape=true,
  keywordstyle=\color{MidnightBlue}\bfseries,
  morekeywords={
    begin,
    addplot,
    addlegendentry,
    documentclass,
    usepackage,
    usetikzlibrary,
    usepgfplotslibrary,
    pgfplotsset,
    nextlist,
    coordinate small,
    draw
  },
}

% latexn environment
\lstnewenvironment{latex}[1][]
  {\lstset{style=latexstyle,#1}}
  {}

\begin{document}

\begin{latex}
\begin{tikzpicture}
\begin{loglogaxis}

\addplot+ [ultra thick, dotted] table[]{
  10  0.2
  100  0.1
  1000  0.05}
coordinate [pos=0.15] (A)
coordinate [pos=0.25] (B);
\addlegendentry{legend 1}
\coordinate (A') at ($(A)!3pt!90:(B)$);
\coordinate (B') at ($(B)!3pt!270:(A)$);
\draw (A')-|(B') node[pos=0.25,above]{$1$}
      node[pos=0.75,right]{$\alpha$};
\draw [shorten <=-3pt,shorten >=-3pt] (A') -- (B');

\end{loglogaxis}
\end{tikzpicture}
\end{latex}

\end{document}

enter image description here

siracusa
  • 13,411
egreg
  • 1,121,712