Tikz 条形图中的文本自动换行吗?

Tikz 条形图中的文本自动换行吗?

我正在尝试在 LaTeX 文档中获取多个条形图(每页 4 个)。问题出在某些图表的标题和标签文本上。它显示了调查结果,一些问题有长文本和响应文本。有没有办法让 tikz 图表“自动”换行/换行问题或响应的文本?我试过手动添加,\\但那不起作用,而且有问题,因为我正在使用电子表格中的宏以编程方式创建 TeX 代码,所以不一定能手动添加\\或其他换行符。如果 PGF/TikZ/LaTeX 可以自动换行以使其适合,那就太理想了。

这是没有显示每页 4 个图表的 MWE,但问题仍然很明显。任何帮助都将不胜感激。谢谢。

\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}

答案1

您的 MWE 中有一些包与情节无关,因此我从下面的代码中删除了它们。我还在\makeatother带有 的块后面添加了缺失的内容\pgfdeclarepatternformonly

要在标题中使用换行符,您可以使用选项title style,要设置 y 刻度的样式,请使用y tick label style。您实际上需要告诉 PGF 您希望文本节点的内容对齐(右对齐、居中对齐或左对齐)并指定具体的(最大)文本宽度。然后会自动应用换行符。

您可能需要稍微调整一下间距。最重要的是,如果 y 标签包含换行符,则需要增加图的高度。

\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}

在此处输入图片描述

相关内容