6

Is there any possibility to set up ganttchart with two-lines labels?

\begin{ganttchart}[inline]{1}{12}
    \gantttitlelist{1,...,12}{1} \\
    \ganttbar{two\\lines}{1}{2} 
\end{ganttchart}

Two-line labels in ganttchart

2 Answers2

8

As described in Manual/automatic line breaks and text alignment in TikZ nodes there are various ways of having line breaks in nodes. The first suggestion, with tabular, will work directly in your case I'd think. If you want to use the second or third method, you need to modify the bar inline label node style, as you're using the inline option. (Otherwise it would be bar label node.)

I think you need to change the bar height manually though, if you want the bar to be taller, so use

\ganttbar[bar height=1]{two\\lines}{1}{2} \\

bar label with two lines

\documentclass{book}
\usepackage{pgfgantt}
\begin{document}
\begin{ganttchart}[
  inline,
  bar inline label node/.append style={
    align=center,
   },
]{1}{12}
    \gantttitlelist{1,...,12}{1} \\
    \ganttbar[bar height=1]{two\\lines}{1}{2} \\
\end{ganttchart}
\end{document}
Torbjørn T.
  • 206,688
  • Thank your for your solution, but it does not resize the bar... – Michael Dorner Mar 08 '18 at 11:57
  • 1
    @MichaelDorner No, I think the node and the bar are independent, in the sense that first the bar is drawn, and then the node is placed on top of it later. But you can set bar height=1, will edit in a few seconds. – Torbjørn T. Mar 08 '18 at 12:02
  • I used this solution, but desired a bar the height of a single line, with small text. To that effect, I used \ganttbar[inline]{\shortstack{$\phantom{.}$ \\ \tiny two \\[-2pt] \tiny lines}}{1}{2} . The phantom full stop is an ad-hoc control for vertical alignment of the two lines. – Rasmus Mar 19 '20 at 09:29
  • 1
    @Rasmus I'd rather suggest adding font=\tiny to the bar inline label node style, and then use \ganttbar{two\\[-2pt]lines}{1}{2} \\ – Torbjørn T. Mar 19 '20 at 20:06
  • @TorbjørnT. : Thank you! – Rasmus Mar 26 '20 at 11:04
1

I found another solution as specified in the documentation with \ganttalignnewline:

\begin{ganttchart}[
    hgrid,
    vgrid,
    newline shortcut=true,
    bar label node/.append style=%
        {align=left}
]{1}{6}
\gantttitle{Title}{6} \\ 
\ganttbar{%
    This is a\ganttalignnewline multi-line text.%
}{1}{3}
\end{ganttchart}