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 am using LaTeX, TikZ and PGFplots to generate a bar chart, in which the xticklabel is read from testdata.dat file.

In the following example, the xticklabel is a single line, i.e., Big-Bed, Small-Bed. Now I want to change them to two-line texts i.e. Big \\ Bed, Small \\ Bed respectively.

Unfortunately, when replacing - with \\ in testdata.dat, I found that there are some errors in compiling. Maybe \\ is not allowed in testdat.dat by fileconcents. Could anyone kindly help me out to change the xticklabel text into two-line texts?

\documentclass{article} 
\usepackage{pgfplots} 
\usepackage{filecontents} 


\begin{filecontents}{testdata.dat} 
Label X-Position Height 
Big-Bed    1    15 
Small-Bed    2    20 
\end{filecontents}

% 
\begin{document} 
\begin{tikzpicture} 
\begin{axis}[ 
    ybar, 
    xtick=data,% crucial line for the xticklabels directive 
    ymin=0, 
    xticklabels from table={testdata.dat}{Label} 
] 

\addplot table [ 
    x=X-Position, 
    y=Height 
] {testdata.dat}; 

\end{axis} 
\end{tikzpicture}
\end{document}
share|improve this question

1 Answer 1

If you want to use line breaks in node names you have to let TikZ switch to some sort of text formatting mode. The easiest is to make an alignment declaration such that it pays attention to the content. Also you can put braces around sensitive node cell contents to protect them (has some limitations probably)

\documentclass{article} 
\usepackage{pgfplots} 
\usepackage{filecontents} 


\begin{filecontents}{testdata.dat} 
Label X-Position Height 
{Big\\BedHotel\\\$\$} 1 15 
{Small\\Bed\&Breakfast} 2 20 
\end{filecontents}

% 
\begin{document} 
\begin{tikzpicture} 
\begin{axis}[ 
    ybar, 
    xtick=data,% crucial line for the xticklabels directive 
    ymin=0, 
    xticklabels from table={testdata.dat}{Label},
        xticklabel style={align=center}
] 

\addplot table [ 
    x=X-Position, 
    y=Height 
] {testdata.dat}; 

\end{axis} 
\end{tikzpicture} 
\end{document}

enter image description here

share|improve this answer

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.