Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I have some data I want to plot in a bar chart where the name on the x-axis should have a % sign in it. So this works:

\documentclass{article}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
  \begin{axis}[
    ybar,
    symbolic x coords={10 perc,20 perc},
    ]
    \addplot coordinates {(10 perc,10) (20 perc,20)};
  \end{axis}
\end{tikzpicture}
\end{document}

But if I try something like:

\documentclass{article}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
  \begin{axis}[
    ybar,
    symbolic x coords={10\%,20\%},
    ]
    \addplot coordinates {(10\%,10) (20\%,20)};
  \end{axis}
\end{tikzpicture}
\end{document}

I get the following error:

ERROR: Missing \endcsname inserted.

--- TeX said ---
<to be read again> 
                   \%
l.8         ]

This happens in and out of math mode.

\\\% causes pdflatex to hang.

Thanks in advance!

share|improve this question
add comment

2 Answers

up vote 3 down vote accepted

LaTeX has a safe percent macro but it uses an @ name so first give it a name without an @.

\documentclass{article}
\usepackage{pgfplots}
\makeatletter
\let\percent\@percentchar
\makeatother

\begin{document}
\begin{tikzpicture}
  \begin{axis}[
    ybar,
    symbolic x coords={10\percent,20\percent},
    ]
    \addplot coordinates {(10\percent,10) (20\percent,20)};
  \end{axis}
\end{tikzpicture}
\end{document}
share|improve this answer
    
Thanks, this almost works! It gives something like this though: !Plot Not sure why the extra x ticks –  nick Jul 26 '12 at 16:24
    
Isn't that exactly the same as your first example but with "perc" replaced by % ?? –  David Carlisle Jul 26 '12 at 16:28
    
hah, right you are, sorry, was miss-reading. this works fine thanks. the only last little niggle is getting there to be a space after the % sign, like if one of the coords was: (Foo 20% Bar,20). thanks again! –  nick Jul 26 '12 at 16:32
    
can't answer your spacing issue (I've never used tikz except for running MWE from questions on this site) If necessary ask another question on site specifically about the spacing –  David Carlisle Jul 26 '12 at 16:35
    
@nick I guess you get a space the same way you always get one after a macro: \percent\ bla (i.e. a \ and a space after the macro) or \percent{} bla. –  Caramdir Jul 26 '12 at 16:40
show 1 more comment

Try this:

\documentclass{article}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
  \begin{axis}[ybar,symbolic x coords={10,20},xticklabel=$\pgfmathprintnumber{\tick}$\,\%]
     \addplot coordinates {(10,10) (20,20)};
  \end{axis}
\end{tikzpicture}
\end{document}

The default for xticklabel is defined by (p. 225 in the pfgplots manual)

\def\axisdefaultticklabel{$\pgfmathprintnumber{\tick}$}
share|improve this answer
    
Ahh, I should have been more clear in my question, but I was trying to keep it simple. Only SOME of my entries have % signs in them. It's actually entries like: {Housing 10%, Housing 20%, No Housing, Something Else}, so the xticklabel trick doesn't (i think) work. –  nick Jul 26 '12 at 16:20
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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