2

I am trying to get multiple bar charts (4 per page) in a LaTeX document. The issue is with the title and label text of some charts. It's showing results of a survey, some questions had long text and response text. Is there a way to get a tikz chart to 'automagically' line break/wrap the text of questions or responses? I've tried manually adding \\ but that doesn't work and is problematic because I am using a macro from spreadsheet to programatically create the TeX code anyway so can't necessarily manually add \\ or other break characters. It would be ideal if PGF/TikZ/LaTeX could automatically wrap text to make it fit.

Here's an MWE without showing the 4 charts per page but the problem is still evident. Any help would be appreciated. Thanks.

\documentclass[a4paper,11pt]{article}
\usepackage{fancyhdr}
\usepackage{graphicx}
\usepackage{setspace}
\usepackage[margin=2cm,bmargin=1.3cm,footskip=5cm,headsep=0.3cm,landscape]{geometry}
%\usepackage[utf8]{inputenc} % caused compilation errors!
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage{subcaption}
\usepackage{booktabs}
\makeatletter
\makeatother
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{width=7cm,compat=1.8}
\usepackage{pgfplotstable}
\usetikzlibrary{patterns}

\begin{document} \pagestyle{fancy}

\makeatletter \g@addto@macro@floatboxreset\centering % automatically center floats \makeatother

\tikzset{ hatch distance/.store in=\hatchdistance, hatch distance=10pt, hatch thickness/.store in=\hatchthickness, hatch thickness=2pt }

    \makeatletter
\pgfdeclarepatternformonly[\hatchdistance,\hatchthickness]{flexible hatch}
{\pgfqpoint{0pt}{0pt}}
{\pgfqpoint{\hatchdistance}{\hatchdistance}}
{\pgfpoint{\hatchdistance-1pt}{\hatchdistance-1pt}}%
{
    \pgfsetcolor{\tikz@pattern@color}
    \pgfsetlinewidth{\hatchthickness}
    \pgfpathmoveto{\pgfqpoint{0pt}{0pt}}
    \pgfpathlineto{\pgfqpoint{\hatchdistance}{\hatchdistance}}
    \pgfusepath{stroke}
}

\begin{tikzpicture} \newcommand\qText{My school provides me with information about my next steps, for example, choosing qualifications for the futue, going to university or other education and training options like apprenticeships, or finding a job.} \newcommand\resA{Yes, I received the right amount of information that was helpful} \newcommand\valA{48} \newcommand\resB{Yes, but it was too much information and was not helpful} \newcommand\valB{23} \newcommand\resC{Yes, but there wasn’t enough information that was helpful} \newcommand\valC{29} \newcommand\resD{No, I haven't received any information} \newcommand\valD{0} \newcommand\resE{} \newcommand\valE{0} \newcommand\ncount{305} \begin{axis}[ axis on top, height=5cm, xmin=0, %width=\textwidth, bar width=0.6cm, %ymajorgrids, tick align=inside, enlarge y limits=true, axis x line=bottom, axis y line=left, y axis line style={opacity=0}, ytick=data, %tickwidth=0pt, enlarge x limits=false, title={`\textbf{\qText}'}, xbar, xlabel={% of Respondents}, symbolic y coords={{\resE}, {\resD}, {\resC}, {\resB}, {\resA}}, %ytick=data, nodes near coords, nodes near coords align={horizontal}, ] \addplot[draw=blue!45, samples=100, pattern=flexible hatch, pattern color=blue!45] coordinates {(\valA,{\resA}) (\valB,{\resB}) (\valC,{\resC}) (\valD,{\resD}) (\valE,{\resE})}; \end{axis} \end{tikzpicture}

\end{document}

Torbjørn T.
  • 206,688
tornadof3
  • 349
  • 1
  • 8

1 Answers1

2

There are a some of packages in your MWE that have nothing to do with the plot, so I removed them from the code below. I also added the missing \makeatother after the block with \pgfdeclarepatternformonly.

To have line breaks in the title, you can use the option title style, to style the y ticks, use y tick label style. You essentially need to tell PGF that you want the contents of the text node to align (right, center or left) and specify a concrete (maximum) text width. Line breaking will then be applied automatically.

You may want to adjust the spacing a bit. Above all, you need to increase the height of the plot if the y labels contain line breaks.

\documentclass[a4paper,11pt]{article}
\usepackage[margin=2cm,bmargin=1.3cm,footskip=5cm,headsep=0.3cm,landscape]{geometry}
\usepackage[T1]{fontenc}

\usepackage{tikz} \usepackage{pgfplots} \pgfplotsset{compat=1.8} %\usepackage{pgfplotstable} \usetikzlibrary{patterns}

\begin{document}

\tikzset{ hatch distance/.store in=\hatchdistance, hatch distance=10pt, hatch thickness/.store in=\hatchthickness, hatch thickness=2pt }

\makeatletter
\pgfdeclarepatternformonly[\hatchdistance,\hatchthickness]{flexible hatch}
{\pgfqpoint{0pt}{0pt}}
{\pgfqpoint{\hatchdistance}{\hatchdistance}}
{\pgfpoint{\hatchdistance-1pt}{\hatchdistance-1pt}}%
{
    \pgfsetcolor{\tikz@pattern@color}
    \pgfsetlinewidth{\hatchthickness}
    \pgfpathmoveto{\pgfqpoint{0pt}{0pt}}
    \pgfpathlineto{\pgfqpoint{\hatchdistance}{\hatchdistance}}
    \pgfusepath{stroke}
}
\makeatother

\noindent% \begin{tikzpicture} \newcommand\qText{My school provides me with information about my next steps, for example, choosing qualifications for the futue, going to university or other education and training options like apprenticeships, or finding a job.} \newcommand\resA{Yes, I received the right amount of information that was helpful} \newcommand\valA{48} \newcommand\resB{Yes, but it was too much information and was not helpful} \newcommand\valB{23} \newcommand\resC{Yes, but there wasn’t enough information that was helpful} \newcommand\valC{29} \newcommand\resD{No, I haven't received any information} \newcommand\valD{0} \newcommand\resE{} \newcommand\valE{0} \newcommand\ncount{305} \begin{axis}[ axis on top, height=7cm, xmin=0, %width=\textwidth, bar width=0.6cm, %ymajorgrids, tick align=inside, enlarge y limits=true, axis x line=bottom, axis y line=left, y axis line style={opacity=0}, ytick=data, %tickwidth=0pt, enlarge x limits=false, title={`\textbf{\qText}'}, title style={align=left, text width=21cm}, xbar, xlabel={% of Respondents}, symbolic y coords={{\resE}, {\resD}, {\resC}, {\resB}, {\resA}}, y tick label style={align=right, text width=9.5cm}, %ytick=data, nodes near coords, nodes near coords align={horizontal}, ] \addplot[draw=blue!45, samples=100, pattern=flexible hatch, pattern color=blue!45] coordinates {(\valA,{\resA}) (\valB,{\resB}) (\valC,{\resC}) (\valD,{\resD}) (\valE,{\resE})}; \end{axis} \end{tikzpicture}

\end{document}

enter image description here

  • Thank you so much for this, worked exactly as I needed. The \makeatother is a confusing thing for LaTeX novices like me as often the commands seem to make no sense. Thanks again. – tornadof3 Nov 20 '21 at 09:50
  • To me, \makeatletter means: "make the @ a letter" (which can be important, since you would normally not be able to use this character, because it is exactly not considered a letter by LaTeX). \makeatother then means: "make the @ this other strange thing again, just like before". Just a mnemonic bridge ... See this question for more information. – Jasper Habicht Nov 20 '21 at 10:03