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 a bar chart, with labels for each column. I would like to break these labels over two lines. I'm not using nodes so the "align" trick doesn't seem to apply.

Minimal example: (from Lyx)

\documentclass[english]{article}
\usepackage[T1]{fontenc}
\usepackage[latin9]{inputenc}
\usepackage{tikz}
\usepackage{pgfplots}


\pgfplotsset{ mygraph/.style={ ybar, nodes near coords, every axis plot post/.append style={ point meta=explicit symbolic } }, mygraph/.default={} }

\makeatother
\usepackage{babel}

\begin{document}
\begin{tikzpicture}
\begin{axis}[mygraph,xmax=10,ymax=10,ytick={0,2,...,10}]
\addplot coordinates {(1,5) [Firstword Secondword]}; <= I want a line break here
\end{axis}
\end{tikzpicture}
\end{document}
share|improve this question
add comment

2 Answers

You should not use \addplot as you are not really doing a plot. Instead use a node as in:

\node [align=left, text width=2cm] at (axis cs:2,6) {Firstword Secondword};

The above acts like a minipage of the specified width and the text is fit in that width. If you prefer to manually specify where the line break should occur then use something like:

\node[align=left] at (axis cs:2,8) {Firstword \\ Secondword};
share|improve this answer
    
Thanks for the fast reply. If I understand right, you mean use \addplot to add the bar, and then manually add the label using \node? Works, but would be great if there was an automatic way of doing it. –  moabi2000 Oct 11 '11 at 16:44
    
@moabi2000: Yes, \addplot is for plotting, and adding a text label is not really plotting. I am not sure what you mean by automated. Perhaps you should post a new question detailing the automation you desire and how you would want to specify it in a MWE. –  Peter Grill Oct 11 '11 at 16:50
    
What I mean is that using my example above, the labels are (if they are on one line) automatically placed just above the appropriate bar. If I rescale the axis (changing ymin for example), or I correct the datapoint, the text is automatically moved also into its new position. Using \node, I have to calculate manually where to put it based on the current text size and y-scale. If I modify the y-scale, or the data value I have to re-calculate and update the coordinates for the node. So not very scalable (in terms of effort) for making many plots with slightly different data. –  moabi2000 Oct 11 '11 at 17:30
1  
Ok, solved. Can't answer my own question yet, but the solution is to add: "every node near coord/.append style={align=center, text width=1cm}" in the preamble. –  moabi2000 Oct 11 '11 at 18:49
add comment

Ok, worked it out based on this question and answer.

The solution is to add:

every node near coord/.append style={
    align=center,
    text width=1cm
}

To the preamble. My test document now looks like this:

\documentclass[english]{article}
\usepackage[T1]{fontenc}
\usepackage[latin9]{inputenc}
\makeatletter
\usepackage{tikz}
\usepackage{pgfplots}

\pgfplotsset{ mygraph/.style={ ybar, nodes near coords, every node near coord/.append style={
    align=center,
    text width=1cm
},
every axis plot post/.append style={ point meta=explicit symbolic }
}, mygraph/.default={} }

\makeatother
\usepackage{babel}

\begin{document}
\begin{tikzpicture}
\begin{axis}[mygraph,xmax=10,ymin=4,ymax=10,ytick={0,2,...,10}]
\addplot coordinates {(6,5) [Two words]};
\addplot coordinates {(4,6) [First word]};
\end{axis}
\end{tikzpicture}
\end{document}

The labels are now automatically broken onto multiple lines, and positioned just above the columns.

share|improve this answer
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.