1

I have made a plot with pgfplots where the symbolic coords contain special characters input via \texttt. When I enable nodes near coords, LaTeX chokes on these special characters.

\documentclass{article}

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

\usepackage{tikz}

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

\begin{document}

\begin{tikzpicture}
\hspace{-0.4cm}
% example derived from §4.5.4 ("Bar Plots") of the pgfplots reference
\begin{axis}[
  height=3cm,
  width=6.8cm,
  xbar,
  symbolic y coords={\texttt{N}, B},
  %nodes near coords,   % <--- uncomment for failure
  ytick=data,
  ytick style={draw=none},
  ymin={[normalized]-0.75},
  ymax={[normalized]2.75},
  ]
  \addplot coordinates {
    (4.1,\texttt{N})
    (5.5,B)
  };
\end{axis}

\end{tikzpicture}

\end{document}

When uncommenting nodes near coords:

! Argument of \XC@definec@lor has an extra }.
<inserted text> 
                \par 
l.30 \end{axis}

...

The same happens with \mathbb instead of \texttt.

Do you have any idea how to make nodes near coords work correctly in my case?

1
  • Better use symbolic y coords={N, B}, yticklabels={\texttt{N}, B} and then place the coordinate at (4.1,N). This is far less error prone. Aug 3, 2022 at 10:08

1 Answer 1

1

If you want to style the tick labels, I would suggest using the option yticklabels to define the labels of the y axis. Also, I would probably just use numeric coordinates to position the data on the y axis which is in general easier to handle.

Maybe also consider using enlarge y limits={0.75} to increase the spacing above and below the y axis rather than changing ymin and ymax.

\documentclass[border=10pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}

\begin{document}

\begin{tikzpicture}
\hspace{-0.4cm}
% example derived from §4.5.4 ("Bar Plots") of the pgfplots reference
\begin{axis}[
  height=3cm,
  width=6.8cm,
  xbar,
%  symbolic y coords={N, B}, 
  yticklabels={\texttt{N}, B},
  nodes near coords,          % <--- uncomment for success
  ytick=data,
  ytick style={draw=none},
  enlarge y limits={0.75},
%  ymin={[normalized]-0.75},
%  ymax={[normalized]2.75},
  ]
  \addplot coordinates {
    (4.1,1)
    (5.5,2)
  };
\end{axis}
\end{tikzpicture}

\end{document}

enter image description here

If you really want to use symbolic coordinates, you can add the option symbolic y coords={N, B} (that is: just use regular strings, no macros) and place the coordiates at (4.1,N) and (5.5,B). Again, you can format the labels with yticklabels={\texttt{N}, B}. Using macros as coordinates is very likely to result in errors.

1
  • 1
    Thank you for your solution! I especially like your approach of using yticklabels together with symbolic y coords, that is definitively much cleaner than using macros as coordinates, while still allowing me to have proper data labels in my CSV tables.
    – Michael F
    Aug 3, 2022 at 10:47

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .