3

I faced a very similar problem to pgfplotsinvokeforeach using parameter when you have the hash symbol as e.g. comment chars. Python uses this character for comments so I want to either use it in my column definition or to comment it out.

I want to give you an idea what I do with Python:

import numpy as np
np.savetxt('test.csv',
           np.column_stack((np.array([0,1]),
                            np.array([50, 49.995]),
                            np.array([0, 0.5726]),
                            np.array([-32.8125, -32.8043])),
           delimiter=',', header='xval,# yval1,# yval2,# yval3')

Keep in mind that Numpy itself adds already a hash symbol at the beginning of the header line. The LaTeX script of interest (MWE) looks the following:

\documentclass{standalone}

\usepackage{filecontents}
\usepackage{pgfplots}

\begin{filecontents}{test.csv}
# xval,# yval1,# yval2,# yval3
0.0000,50.0000,0.0000,-32.8125
1.0000,49.9950,0.5726,-32.8043
\end{filecontents}


\begin{document}

\begin{tikzpicture}
\begin{axis}
\addplot
table[x=1, y=2, col sep=comma, comment chars=#] {test.csv};
table[x={# xval}, y={# yval2}, col sep=comma] {test.csv};
\end{axis}
\end{tikzpicture}

\end{document}

None of these two examples seems to work probably because this hash symbol is one of the reserved symbols in LaTeX. Escaping or doubling the hash does not help neither. Any help is appreciated.

strpeter
  • 5,215

1 Answers1

4

With comment chars=\# it works. Of course, that means the first row is skipped (it's commented out), so you have a table without a header row. In that case I suppose you might have to use x index/y index instead of explicit column names, e.g.

\documentclass{standalone}

\usepackage{filecontents}
\usepackage{pgfplots}

\begin{filecontents}{test.csv}
# xval,# yval1,# yval2,# yval3
0.0000,50.0000,0.0000,-32.8125
1.0000,49.9950,0.5726,-32.8043
\end{filecontents}


\begin{document}

\begin{tikzpicture}
\begin{axis}
\addplot table[x index=0, y index=2, col sep=comma, comment chars=\#] {test.csv};
\end{axis}
\end{tikzpicture}

\end{document}
Torbjørn T.
  • 206,688
  • Perfect, I forgot about x index etc. Thank you! – strpeter Jun 01 '18 at 16:47
  • So as I understood, there is no way to write something like x={# xval} or even x={# x_val} (or with escaping these characters) since these characters are forbidden in variables? – strpeter Jun 01 '18 at 23:17
  • @strpeter I think you're right about that. You could of course tell numpy not to comment out the header line with comments='' so that this isn't a problem. – Torbjørn T. Jun 02 '18 at 08:07