5

I have a file which contains column headings such as AAA-BBB or something to the effect of CC_DD_8734-20°.

How should I deal with this if I just want to use such a file?

Or should I prepare each set of data for with x-y columns? (which would be very cumbersone, but definitely work)

MWE

\documentclass[
a4paper
]{scrartcl}

\usepackage{
    lmodern,
    tikz,
    pgfplots,
    pgfplotstable
    }
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}

\begin{filecontents}{data.csv}
AA-BB;Min;Max
CC_DD-01;-28.035191;19.114903
CC_DD-02°;-26.760864;22.937884
CC_DD-03°;-29.309518;16.566249
CC_DD-04;-28.035191;20.38923
CC_DD-05;-26.760864;19.114903
CC_DD-06;-25.486538;16.566249
\end{filecontents}

\pgfplotstableread[col sep=semicolon]{data.csv}{\tableabcdef}

\begin{document}
Test.
\begin{center}
\centering
\begin{tikzpicture}
\begin{axis}[
xbar,
xlabel={Values},
ylabel={Subject},
xmin=-4,
ytick=data,
nodes near coords, nodes near coords align={horizontal},
yticklabels from table={\tableabcdef}{AA-BB},
]
\foreach \i in {Min,Max}{
\addplot table [meta=AA-BB, y expr=\coordindex, x=\i] {\tableabcdef};}
\end{axis}
\end{tikzpicture}
\end{center}
\end{document}
henry
  • 6,594
  • First, just see it there is a problem. If there is, show us the test (MWE). – John Kormylo Jul 10 '14 at 13:19
  • @JohnKormylo Did you mistype if? It would make much more sense then. :) Anyway, I updated the op with an MWE. – henry Jul 10 '14 at 15:29
  • The only thing it has a problem with is the CC_DD-01 etc. I don't suppose you could just edit the "CC_DD-" out? – John Kormylo Jul 10 '14 at 23:01
  • 1
    I don't know if this is adequare, but it will at least run. \pgfplotstableread[col sep=semicolon,ignore chars={C,_,D}]{data.csv}{\tableabcdef} I suspect what you really want is to convert the first - into a ; – John Kormylo Jul 10 '14 at 23:13
  • @JohnKormylo Wow that seems to all there is to it. That is great. Well actually I kind of made a stupid error as i forgot to include the °-sign but I found out that it doesn't matter, only the underscores seem to cause grief. – henry Jul 11 '14 at 06:16
  • About the °-sign, this is why I updated the MWE. Anyway, if you post your comment as a reply I'd mark it as solved. At this moment the problem seems to be resolved. (Maybe include a note about the ignore chars key, as I was not aware of that and maybe other aren't either.) – henry Jul 11 '14 at 06:17
  • @JohnKormylo Actually, although the \circ-sign seems to be omitted automatically, I tried to include it in the list for ignored characters, just for the sake of it. But I wasn't successful so far (^\circ, and then different versions of braces around them...). If you have any idea about that, I am very interested to hear it. :) – henry Jul 11 '14 at 07:41
  • Actually, I've been having some success using AA-BB as is. Not quite there yet. – John Kormylo Jul 11 '14 at 12:01

2 Answers2

3

A bit clumsy, but it works. The filecontents environment should go before the loading of inputenc.

\begin{filecontents}{\jobname.csv}
AA-BB;Min;Max
CC_DD-01;-28.035191;19.114903
CC_DD-02°;-26.760864;22.937884
CC_DD-03°;-29.309518;16.566249
CC_DD-04;-28.035191;20.38923
CC_DD-05;-26.760864;19.114903
CC_DD-06;-25.486538;16.566249
\end{filecontents}

\documentclass[
a4paper
]{scrartcl}

\usepackage{
    lmodern,
    tikz,
    pgfplots,
    pgfplotstable
    }
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}

% for the ° character
\usepackage{newunicodechar}
\newunicodechar{°}{\ensuremath{^\circ}}

% make the underscore into a printable symbol,
% but first remember its meaning
\edef\rememberuscatcode{%
  \catcode`_=\the\catcode`_ 
  \let\noexpand\rememberuscatcode\relax
}
\catcode`_=12

\pgfplotstableread[col sep=semicolon]{\jobname.csv}{\tableabcdef}

% revert the underscore to its former meaning
\rememberuscatcode

\begin{document}
Test.

\begin{center}
\centering
\begin{tikzpicture}
\begin{axis}[
xbar,
xlabel={Values},
ylabel={Subject},
xmin=-4,
ytick=data,
nodes near coords, nodes near coords align={horizontal},
yticklabels from table={\tableabcdef}{AA-BB},
]
\foreach \i in {Min,Max}{
\addplot table [meta=AA-BB, y expr=\coordindex, x=\i] {\tableabcdef};}
\end{axis}
\end{tikzpicture}
\end{center}
\end{document}

enter image description here

Note: I used \jobname in order not to clobber my files. Use whatever name you prefer.

egreg
  • 1,121,712
2

Your question is very similar to How to use underscores with pgfplotstable? in that you have some plain string as input and you want to typeset that string automatically "as-is but with some math-mode adjustments".

The solution to the linked question is to apply search-and-replace to the plain text string such that it can be typeset by TeX. The same approach can be used here as well:

\documentclass{standalone}

\usepackage{
    pgfplots,
    pgfplotstable
    }

\pgfplotsset{compat=1.5}

\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}

\pgfplotstableread[col sep=semicolon]{data.csv}{\tableabcdef}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
xbar,
xlabel={Values},
ylabel={Subject},
xmin=-4,
ytick=data,
nodes near coords, nodes near coords align={horizontal},
yticklabel={%
    \pgfplotstablegetelem{\ticknum}{AA-BB}\of{\tableabcdef}%
    \def\marshal{\pgfplotsutilstrreplace{_}{\_}}%
    \expandafter\marshal\expandafter{\pgfplotsretval}%
    \def\marshal{\pgfplotsutilstrreplace{°}{$^\circ$}}%
    \expandafter\marshal\expandafter{\pgfplotsretval}%
    %
    \texttt{\pgfplotsretval}%
},
enlarge x limits={upper,0.3},
]
\foreach \i in {Min,Max}{
\addplot table [meta=AA-BB, y expr=\coordindex, x=\i] {\tableabcdef};}
\end{axis}
\end{tikzpicture}
\end{document}

enter image description here

What it does is

  • define yticklabel such that it takes \ticknum on input and reads the corresponding row from the input table.
  • the value will be stored in \pgfplotsretval - as-is.
  • then it runs \pgfplotsutilstrreplace{°}{$^\circ$}{<value>} and the same for the underscore. This is the same approach as in How to use underscores with pgfplotstable?, except that is uses expansion magic because <value> must be expanded once before the search-and-replace starts (compare Where do I start LaTeX programming? for details)
  • finally, it typesets the label with type writer font (hoping that this is what you wanted -- otherwise you may want to replace the minus sign by ${-}$ as well).

in addition, I added

  • \pgfplotset{compat=1.5} such that the y label is positioned correctly
  • enlarge limits

My answer is actually less pgfplots and more plain TeX. If someone wants to study the approach, he might want to reduce the problem to something which is less involved. Here is such a reduced case.

\documentclass{standalone}

\usepackage{pgfplots}

\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}

\begin{document}
\def\pgfplotsretval{CC_DD-03°}

% FAILS:
% \pgfplotsretval

        \def\marshal{\pgfplotsutilstrreplace{_}{\_}}%
        \expandafter\marshal\expandafter{\pgfplotsretval}%
        \def\marshal{\pgfplotsutilstrreplace{°}{$^\circ$}}%
        \expandafter\marshal\expandafter{\pgfplotsretval}%

\ttfamily
\pgfplotsretval
\end{document}

Nevertheless, I have the impression that special characters in data files need some smart handling as soon as strings are to be read from data files. This might deserve some thoughts on how to do this "properly" in pgfplotstable. If someone has a good idea: let me know (probably by email, my address is in the manual of pgflotstable).