我正在使用 pgfgrantt 包在 Overleaf 中准备甘特图。我使用条形图和里程碑顶部移位选项将它们向上移动并挤压得更近。然而,这会在图表底部留下很多未使用的行,如附图所示。我该如何摆脱这些行?下面的代码可以重现所附的图表。
\begin{ganttchart}[
vgrid,
hgrid,
milestone top shift=-2.45,
milestone right shift=0.3,
group top shift = -0.05,
bar height = 0.2,bar label node/.append style={align=right}
]{1}{24}
\gantttitle{2022}{3} \gantttitle{2023}{12}
\gantttitle{2024}{9} \\
\gantttitlelist{10,11,12}{1} \gantttitlelist{1,...,12}{1} \gantttitlelist{1,...,9}{1}\\
\ganttgroup{a}{1}{11.5} \\
\ganttbar[bar top shift=-0.6]{b}{1}{2.5}{jj} \\
\ganttlinkedbar[bar height=0.2, bar top shift=-1.2]{c}{3.5}{8.5} \\
\ganttlinkedbar[bar top shift=-1.8]{d}{8.5}{10.5} \\
\ganttlinkedmilestone{e}{11}
%%
\ganttgroup[group top shift = -1.6]{2\\f}{7}{15} \\
\ganttbar[bar height = 0.4, bar top shift=-2.1]{g \\[-3pt]h}{7}{8}{jj} \\
\ganttlinkedbar[bar height=0.4, bar top shift=-2.2]{i \\[-3pt] j}{9}{11} \\
\ganttlinkedbar[bar height = 0.4, bar top shift=-2.3]{k \\[-3pt] l}{12}{14} \\
\ganttlinkedmilestone[milestone top shift = -2.6]{m}{15}
%%
\ganttgroup[group top shift = -1.6]{n}{3}{23} \\
\ganttbar[bar height = 0.2, bar top shift=-2.1]{p}{3}{5}{jj} \\
\ganttlinkedmilestone[milestone top shift = -2.7]{o}{6} \\
\ganttlinkedbar[bar height=0.2, bar top shift=-3.1]{q}{15}{18} \\
\ganttlinkedbar[bar height = 0.4, bar top shift=-3.5]{r \\[-3pt] s}{19}{22} \\
\ganttlinkedmilestone[milestone top shift = -3.8]{t}{23}
\end{ganttchart}
答案1
图表的高度是通过将\ganttbar
、\ganttgroup
、 ... 命令的数量乘以固定长度 而自动计算出来y unit chart
的。因此,即使您通过手动移动某些行来挤压它们,图表的高度也不会受到影响。我建议将其设置y unit chart
为较小的值,并且不要手动移动条形图。
\begin{ganttchart}[..., y unit chart = 7mm, ...]
其他问题:您不应该使用\\
来断开标签中的线条,但是\ganttalignnewline
。(或者,您可以将其设置newline shortcut
为 false,但随后必须使用\ganttnewline
来分隔图表中的线条)。
\ganttbar{g \ganttalignnewline[-3pt]h}{7}{8}{jj}
最后,换行符似乎在标签中无论如何都不起作用\ganttgroup
。如果您确实需要两行,请将标签排版为\savebox
。
\newsavebox\twof
\savebox\twof{\bfseries\begin{tabular}{@{}r@{}}2\\[-3pt]f\end{tabular}}
...
\ganttgroup{\usebox\twof}{7}{15}
\documentclass{article}
\usepackage{pgfgantt}
\begin{document}
\newsavebox\twof
\savebox\twof{\bfseries\begin{tabular}{@{}r@{}}2\\[-3pt]f\end{tabular}}
\begin{ganttchart}[
vgrid,
hgrid,
bar height = 0.2,
bar label node/.append style={align=right},
y unit chart = 7mm
]{1}{24}
\gantttitle{2022}{3} \gantttitle{2023}{12} \gantttitle{2024}{9} \\
\gantttitlelist{10,11,12}{1} \gantttitlelist{1,...,12}{1} \gantttitlelist{1,...,9}{1}\\
\ganttgroup{a}{1}{11.5} \\
\ganttbar{b}{1}{2.5}{jj} \\
\ganttlinkedbar{c}{3.5}{8.5} \\
\ganttlinkedbar{d}{8.5}{10.5} \\
\ganttlinkedmilestone{e}{11} \\
%%
\ganttgroup{\usebox\twof}{7}{15} \\
\ganttbar[bar height = 0.4]{g \ganttalignnewline[-3pt]h}{7}{8}{jj} \\
\ganttlinkedbar[bar height=0.4]{i \ganttalignnewline[-3pt] j}{9}{11} \\
\ganttlinkedbar[bar height = 0.4]{k \ganttalignnewline[-3pt] l}{12}{14} \\
\ganttlinkedmilestone{m}{15}\\
%%
\ganttgroup{n}{3}{23} \\
\ganttbar{p}{3}{5}{jj} \\
\ganttlinkedmilestone{o}{6} \\
\ganttlinkedbar{q}{15}{18} \\
\ganttlinkedbar{r \ganttalignnewline[-3pt] s}{19}{22} \\
\ganttlinkedmilestone{t}{23}
\end{ganttchart}
\end{document}