1

Under normal circumstances \ch{AB_2} is rendered as $AB_2$, but not inside the pgfplot. I am getting the error message Package PGF Math Error: Could not parse input '' as a floating point number and labels overlap. Is it somehow possible to make these 2 packages to be friends. I am still learning PGF, therefore such naive question. I would appreciate your any input.

\documentclass{standalone}
\usepackage{pgfplots,chemformula,filecontents}
\pgfplotsset{compat=newest}
\begin{filecontents}{data.dat}
    N   system     A1          C2       Exp
    1     CH2     -0.70      -0.82     -0.53 
    2     NH3     -0.97      -1.58      0.00 
    3    H2HO     -0.96      -1.53     -0.06 
    4    SiH2      0.11      -0.92     -0.60 
\end{filecontents}
\begin{document}
\begin{tikzpicture}
    \begin{axis}[xlabel=eV,ylabel=eV]
        \addplot[
                visualization depends on={value \thisrow{system} \as \label},           
                scatter, only marks,
                scatter src=explicit symbolic,
                nodes near coords*={\ch{\label}}]
         table[x=Exp,y=A1]{data.dat};
    \end{axis}
\end{tikzpicture}
\end{document}
4

This is a purely pgfplots problem. You're mixing the point meta (synonym for scatter src) and visualization depends on. But point meta is used to pass extra data in a standard way, while visualization depends on is used to give extra data without interfering with point meta, and how you use it is completely up to you.

Solution 1 Pass the labels as point meta (here with scatter src):

\documentclass{standalone}
\usepackage{pgfplots,chemformula,filecontents}
\pgfplotsset{compat=newest}
\begin{filecontents}{data.dat}
    N   system     A1          C2       Exp
    1     CH2     -0.70      -0.82     -0.53 
    2     NH3     -0.97      -1.58      0.00 
    3    H2HO     -0.96      -1.53     -0.06 
    4    SiH2      0.11      -0.92     -0.60 
\end{filecontents}
\begin{document}
\begin{tikzpicture}
    \begin{axis}[xlabel=eV,ylabel=eV]
        \addplot[
                scatter, only marks,
                scatter src=explicit symbolic,
                nodes near coords={\expandafter\ch\expandafter{\pgfplotspointmeta}},
                ]
         table[x=Exp,y=A1,meta=system]{data.dat};
    \end{axis}
\end{tikzpicture}
\end{document}

As you can see I used the meta key to specify the point meta column. The \expandafter trickery is apparently necessary to typeset the chemical formula with \ch if the formula text is in a macro (like \label here). Also note that there is no asterisk after nodes near coords: otherwise pgfplots will apply a default style to the nodes that expects numerical values in point meta.

Solution 2 Pass labels with visualization depends on:

\documentclass{standalone}
\usepackage{pgfplots,chemformula,filecontents}
\pgfplotsset{compat=newest}
\begin{filecontents}{data.dat}
    N   system     A1          C2       Exp
    1     CH2     -0.70      -0.82     -0.53
    2     NH3     -0.97      -1.58      0.00
    3    H2HO     -0.96      -1.53     -0.06
    4    SiH2      0.11      -0.92     -0.60
\end{filecontents}
\begin{document}
\begin{tikzpicture}
    \begin{axis}[xlabel=eV,ylabel=eV]
        \addplot[
                scatter, only marks,
                nodes near coords={\expandafter\ch\expandafter{\label}},
                visualization depends on={value \thisrow{system} \as \label},           
                ]
         table[x=Exp,y=A1]{data.dat};
    \end{axis}
\end{tikzpicture}
\end{document}

Note that I'm not using scatter src here (i.e. no point meta). In both this solution and the previous one we have some overlap of the text labels...

Solution 3 Use visualization depends on to give the label text and point meta to set the label position:

\documentclass{standalone}
\usepackage{pgfplots,chemformula,filecontents}
\pgfplotsset{compat=newest}
\begin{filecontents}{data.dat}
    N   system     A1          C2       Exp    Position
    1     CH2     -0.70      -0.82     -0.53   1
    2     NH3     -0.97      -1.58      0.00   1
    3    H2HO     -0.96      -1.53     -0.06   -1
    4    SiH2      0.11      -0.92     -0.60   -1
\end{filecontents}
\begin{document}
\begin{tikzpicture}
    \begin{axis}[xlabel=eV,ylabel=eV]
        \addplot[
                scatter, only marks,
                scatter src=explicit,
                nodes near coords align=vertical,
                nodes near coords={\expandafter\ch\expandafter{\label}},
                visualization depends on={value \thisrow{system} \as \label},           
                ]
         table[x=Exp,y=A1,meta=Position]{data.dat};
    \end{axis}
\end{tikzpicture}
\end{document}

I added a data column with a value as expected by the key nodes near coords align=vertical. Note the use of scatter src=explicit: we give point meta from the table data but this time as numerical data.

| improve this answer | |
  • Dear Jeremie, I tried all your solutions, rather different results are produced: 1) is not possible to compile with xelatex; 2) labels overlap; 3) a clear plot. Do you know what could wrong with (1)? – yarchik Feb 8 '18 at 15:18
  • @yarchik, I have no errors when compiling the three examples with xelatex. Which error do you get? Note that solution 1 also has the overlap anyway. For solution 3, does "clear plot" mean it's good, or that nothing is shown? (in that case, do you get an error message?) – jeremie Feb 8 '18 at 15:33
  • @yarchik, I just noticed I dropped the last line (\end{document}) in the solution 1 copy-paste (fixed now). – jeremie Feb 8 '18 at 15:41
  • Everything is fine ! – yarchik Feb 8 '18 at 15:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.