# Linebreak in ticks-labels

I'm using \symbolic y coords in a xbar-plot. Some of these coords are very long. There is a solution using nodes which is very ... hardcoded. And it would be nice to have it automated.

1. How do I generate line breaks in ticks labels?
2. Is there a way without using nodes?

MWE:

\documentclass[12pt,a4paper]{standalone}
\usepackage{pgfplots}

\begin{document}

\begin{tikzpicture}
\pgfplotsset{every axis legend/.append style={at={(0.5,1.025)}, anchor=south,semithick,legend columns=3}}
\begin{axis}[   xlabel=x-label,
xbar,
xmin=0,
enlarge y limits=0.2,
height=0.5\textwidth,
ytick=data,
bar width=7pt,
symbolic y coords={y label 1,y label 2,very very very very very long y label 3,thelongestylabeleverwrittenasoneword},]

{(5.3987,y label 1)(16.1961,y label 2)(32.081,very very very very very long y label 3)(41.9026,thelongestylabeleverwrittenasoneword)};
{(2.3917,y label 1)(7.1751,y label 2)(10.153,very very very very very long y label 3)(15.5834,thelongestylabeleverwrittenasoneword)};
{(0.4881,y label 1)(1.4643,y label 2)(4.361,very very very very very long y label 3)(2.7435,thelongestylabeleverwrittenasoneword)};
\end{axis}
\end{tikzpicture}

\end{document}


-
Welcome to TeX.sx! Please add a minimal working example (MWE) that illustrates your problem. Also, you don't have to sign with your name since it automatically appears in the lower right corner of your post. –  Joseph Wright Nov 23 '11 at 9:55
That is not an MWE. It contains many irrelevant packages. Please only include packages that are relevant for the problem. –  N.N. Nov 23 '11 at 11:47

I would approach this plot a bit differently: Instead of using symbolic y coords, I would manually specify the labels using yticklabels={<First>,<Second>,...}, which makes it possible to specify the line breaks explicitly by using something like yticklabels={Primär-\\energieverbrauch,...}. This will only work if you also specify an align for the labels, by setting yticklabel style={align=right}, for instance.

I would also load the data into a table first, by loading the pgfplotstable package (which ships with PGFplots) and then using \pgfplotstableread{<data table>}<macro name>. You can then say \addplot table [x=<column name>] <macro name> to plot a column. If your data table does not contain a numerical ID field that can be used for the y position, you can say \addplot table [x=<column name>, y=\coordindex] ..., which will sequentially assign a number to each row in the table.

\documentclass[12pt,a4paper]{article}
\usepackage{lmodern}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[ngerman]{babel}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\pgfplotsset{width=0.9\textwidth,compat=1.4}

Name,A,B,C
Stromverbrauch, 5398.7, 2391.7, 488.1
Primärenergieverbrauch, 16196.1, 7175.1, 1464.3
Primärenergieeinsparung, 10153, 15583.4, 4361
Kälteproduktion, 32081, 41902.6, 2743.5
}\datatable
\begin{document}

\begin{tikzpicture}
\pgfplotsset{every axis legend/.append style={at={(0.5,1.025)}, anchor=south,semithick,legend columns=3}}
\begin{axis}[   xlabel=Energie {[kWh]},
xbar,
xmin=0,
enlarge y limits=0.2,
height=0.5\textwidth,
ytick=data,
yticklabels={
Stromverbrauch,
Primärenergie-\\verbrauch,
Primärenergie-\\einsparung,
Kälteproduktion
},
yticklabel style={align=right},
bar width=7pt,
]

\addplot [fill=red] table [x=A,y expr=\coordindex] {\datatable};
\addplot [fill=green] table [x=B,y expr=\coordindex] {\datatable};
\addplot [fill=blue] table [x=C,y expr=\coordindex] {\datatable};
\end{axis}
\end{tikzpicture}

\end{document}


Generally, you can set the maximum width the labels may take up by saying yticklabel style={text width=<length>}, forcing the text to wrap around. It seems, however, that this doesn't work with umlauts, even if you load [T1]{fontenc}. Here's an example that compiles correctly with lualatex instead of pdflatex, replacing the inputenc and fontenc packages with fontspec:

\documentclass[12pt,a4paper]{article}
\usepackage{lmodern}
\usepackage{fontspec}
\usepackage[ngerman]{babel}
\usepackage{pgfplots}
\pgfplotsset{width=0.9\textwidth,compat=1.4}

\begin{document}

\begin{tikzpicture}
\pgfplotsset{every axis legend/.append style={at={(0.5,1.025)}, anchor=south,semithick,legend columns=3}}
\begin{axis}[   xlabel=Energie {[kWh]},
xbar,
xmin=0,
enlarge y limits=0.2,
height=0.5\textwidth,
ytick=data,yticklabel style={text width=3cm,align=right},
bar width=7pt,
symbolic y coords={Stromverbrauch,Primärenergieverbrauch,Primärenergieeinsparung,Kälteproduktion},]