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.

I use pgfplot for plotting (obviously). I load a table from a file, but I have the problem that my columns can have a dollar sign in them and then LaTeX complains:

! Extra }, or forgotten $.

\documentclass{beamer}

\usepackage{pgfplots}
\pgfplotsset{compat=1.5}

\def\myplot#1{
\foreach \file in {#1} {
        \begingroup
        \pgfplotstableread{\file}\report
        \pgfplotstableforeachcolumn \report \as {\colname}{ 
            \ifnum\pgfplotstablecol=0 %
            \else
    \begin{frame}
        \begin{tikzpicture}
        \begin{axis}[
            title=\file,
            xlabel={Recall},
            ylabel={Precision},
            ymin=0,
            ymax=1,
            legend entries={\colname},
        ]

            \addplot table[ y = \colname] {\file};



        \end{axis}
        \end{tikzpicture}
    \end{frame}
    \fi
}
        \endgroup
    }
}

\begin{document}

\myplot{reports/report.csv}


\end{document}

Report.csv:

Recall  org/apache/tools/ant/helper/ProjectHelperImpl$ProjectHandler/results.json
0.0 1.0
0.1 0.5252788975806337
0.2 0.5242015649572856
0.30000000000000004 0.5212258417099329
0.4 0.5148570391086534
0.5 0.5132710775465511
0.6000000000000001  0.4835666797890683
0.7000000000000001  0.4736173712634098
0.8 0.4705726571519701
0.9 0.46958229692873316
1.0 0.46958229692873316

I tried using \texttt, but then it complains as well with a different message:

! Missing \endcsname inserted.

It seems to me that I somehow need to escape the $, but how, seeing that I get them from a file?

share|improve this question
1  
Welcome to TeX.SX! You can have a look on our starter guide to familiarize yourself further with our format. –  Jubobs Jun 14 '13 at 12:00
1  
This is a shot in the dark, since you didn't post any minimal working example (MWE) we could work on, but perhaps substituting \expandafter\detokenize\colname for \texttt\colname would solve the problem. –  Jubobs Jun 14 '13 at 12:05
2  
It would be great if you could edit your question and complete the code to form a complete minimal example document (starting from \documentclass, and including an example data file). –  Jake Jun 14 '13 at 12:09

1 Answer 1

up vote 6 down vote accepted

The problem is that the dollar sign opens math mode, but it's never closed. Since you only want to print the dollar sign, you'll somehow want to replace the $ in the string with \string$.

You can do this using the xstring package by writing \StrSubstitute{\colname}{$}{\string$}[\stripped].

\documentclass{beamer}

\usepackage{pgfplots}
\pgfplotsset{compat=1.5}
\usepackage{xstring}


\def\myplot#1{
\foreach \file in {#1} {
        \begingroup
        \pgfplotstableread{\file}\report
        \pgfplotstableforeachcolumn \report \as {\colname}{ 
            \ifnum\pgfplotstablecol=0 %
            \else
            \StrSubstitute{\colname}{$}{\string$}[\stripped]
    \begin{frame}
        \begin{tikzpicture}
        \begin{axis}[
            title=\file,
            xlabel={Recall},
            ylabel={Precision},
            ymin=0,
            ymax=1,
            legend entries={\stripped},
            legend style={font=\tiny,
                anchor=north,
                yshift=-8ex,
                at={(current axis.south)}},
        ]

            \addplot table[ y = \colname] {\report};


        \end{axis}
        \end{tikzpicture}
    \end{frame}
    \fi
}
        \endgroup
    }
}

\begin{document}

\myplot{report.csv}


\end{document}
share|improve this answer
    
Although your code solves the problem, I don't get any dollar sign in the caption when I compile it. I use PGF version: 2.10. –  Jubobs Jun 14 '13 at 12:50
    
@Jubobs: Hm, that's strange. I'm using the TikZ CSV version, but I don't think it's an issue with that. Can you try using the article class (remove the frame environment for that) and printing the content of \stripped before the tikzpicture? –  Jake Jun 14 '13 at 12:59
    
Just say \StrSubstitute{\colname}{$}{\string$}[\stripped] –  egreg Jun 14 '13 at 13:02
    
@egreg: Nice, that's much better. Thanks! –  Jake Jun 14 '13 at 13:07
1  
@Jake sorry my bad, just ignore that last comment –  kutschkem Jun 14 '13 at 13:26

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.