StrSubstitute 与 pgfplots 配合使用效果不佳

StrSubstitute 与 pgfplots 配合使用效果不佳

这个问题与我之前的问题相关这里,但现在略有不同。我现在有一个函数,它应该接受两个多值参数。一个包含我想要绘制的列。如果我像上一个问题一样在列名中转义美元符号,则尝试制作图例会导致一个图例条目包含整个逗号分隔的字符串。但是,尝试像下面的代码示例一样解决这个问题会导致 LaTeX 抱怨 \stripped 是一个未定义的命令序列。我做错了什么?

\documentclass{beamer}

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

\def\multiplot#1 of #2{


    \begin{frame}
        \begin{tikzpicture}
        \begin{axis}[
            title={Title},
            xlabel={Recall},
            ylabel={Precision},
            ymin=0,
            ymax=1,
            legend style={font=\tiny,
                anchor=north,
                yshift=-8ex,
                at={(current axis.south)}},
        ]
\foreach \fileRep in {#2} {
            \foreach \colname in {#1} {
            \addplot table[ y =\colname] {\fileRep};
    \StrSubstitute{\colname}{$}{\string$}[\stripped]
    \addlegendentry{\stripped}
    }
    }
        \end{axis}
        \end{tikzpicture}
    \end{frame}
}

\begin{document}

\multiplot{org/apache/tools/ant/helper/ProjectHelperImpl$ProjectHandler/results.json, org/apache/tools/ant/taskdefs/Javadoc$SourceFile/results.json} of {reports/report.csv}

\end{document}

示例文件:

Recall  org/apache/tools/ant/helper/ProjectHelperImpl$ProjectHandler/results.json   org/apache/tools/ant/taskdefs/Javadoc$SourceFile/results.json
0   0.5 0.6

答案1

如果您有在循环内生成的图例条目,则必须扩展循环变量的内容。您只需将其替换\addlegendentry\addlegendentryexpanded

\documentclass{beamer}

\usepackage{xstring}
\usepackage{pgfplots}
\pgfplotsset{compat=1.5}
\usepackage{filecontents}
\begin{filecontents}{report.csv}
Recall  org/apache/tools/ant/helper/ProjectHelperImpl$ProjectHandler/results.json   org/apache/tools/ant/taskdefs/Javadoc$SourceFile/results.json
0   0.5 0.6
\end{filecontents}

\def\multiplot#1 of #2{


    \begin{frame}
        \begin{tikzpicture}
        \begin{axis}[
            title={Title},
            xlabel={Recall},
            ylabel={Precision},
            ymin=0,
            ymax=1,
            legend style={font=\tiny,
                anchor=north,
                yshift=-8ex,
                at={(current axis.south)}},
        ]
\foreach \fileRep in {#2} {
            \foreach \colname in {#1} {
            \addplot table[ y =\colname] {\fileRep};
    \StrSubstitute{\colname}{$}{\string$}[\stripped]
    \addlegendentryexpanded{\stripped}
    }
    }
        \end{axis}
        \end{tikzpicture}
    \end{frame}
}

\begin{document}

\multiplot{org/apache/tools/ant/helper/ProjectHelperImpl$ProjectHandler/results.json, org/apache/tools/ant/taskdefs/Javadoc$SourceFile/results.json} of {report.csv}

\end{document}

相关内容