26

Is it possible to reduce the default vertical space between bars in pgfgantt? The manual specifies "last line height" but there appears to be no "line height" option.

\documentclass[a4paper,11pt]{article}
\usepackage{pgfgantt}
\begin{document}
\section{}


\begin{tikzpicture}[x=3cm, y=0.75cm]  
\begin{ganttchart}[bar={fill=red!50}]{5}
\gantttitle{2012--2017}{5} \\
\gantttitlelist{1,...,5}{1} \\
\ganttbar{PhD1}{1}{3.5} \\
\ganttbar{PhD2}{2.5}{5} \\
\end{ganttchart}
\end{tikzpicture}
\end{document}

4 Answers 4

18

I just submitted pgfgantt v2.0 to CTAN. Among several other new features, this version introduces the three keys x unit, y unit title and y unit chart. These keys should allow you to use different line heights in the title and the remainder of the chart.

2
  • 5
    I don't think these keys impact spacing between bars, though, which was the original question.
    – Jesse Chan
    Commented May 24, 2018 at 1:59
  • By adjusting bar top shift and bar height, I was able to produce the spacing I wanted. For example, \begin{ganttchart}[bar top shift = 0.1, bar height = 0.8], etc.
    – rickhg12hs
    Commented Apr 30, 2019 at 10:09
15

The best I could do is to adjust the y-scale to y=0.50cm, and add bar top shift=-0.1, bar height=0.6 to get the horizontal bars the original height. This also necessitated tweaking the title setup, so am thinking that there must be a better way, but hopefully this helps:

\documentclass[a4paper,11pt]{article}
\usepackage{pgfgantt}
\begin{document}
\section{}

\begin{tikzpicture}[y=0.75cm]  
\begin{ganttchart}[bar={fill=red!50},vgrid, hgrid]{5}
\gantttitle{2012--2017}{5} \\
\gantttitlelist{1,...,5}{1} \\
\ganttbar{PhD1}{1}{3.5} \\
\ganttbar{PhD2}{2.5}{5} \\
\end{ganttchart}
\end{tikzpicture}
%
\begin{tikzpicture}[y=0.50cm]  
\begin{ganttchart}[bar={fill=red!50},vgrid, hgrid,
    title height=.75, title top shift=0,
    title label anchor={below=-1.5ex},
    bar top shift=-0.1, bar height=0.6]{5}
\gantttitle{2012--2017}{5} \\
\gantttitlelist{1,...,5}{1} \\
\ganttbar{PhD1}{1}{3.5} \\
\ganttbar{PhD2}{2.5}{5} \\
\end{ganttchart}
\end{tikzpicture}
\end{document}

enter image description here

The x=3cm was removed just to be able to show a before and after.

1
  • Your sample code doesn't compile cleanly with the latest package versions. But tweaking bar height and bar top shift still works nicely. I got good results with the following settings y unit title=0.5cm, y unit chart=0.5cm, bar height=0.7, bar top shift=0.15, title top shift=0, Commented Feb 13, 2023 at 13:39
13

in v4.0 from 2013/06/01 you can use something like:

\begin{ganttchart}[y unit chart=2cm]{0}{10}
\ganttbar{magic}{3}{8}\\
\ganttbar{magic2}{2}{8}\\
\end{ganttchart}

see page 7 official documentation

2
  • Hi, welcome to TeX.sx. This is just what is mentioned in the accepted answer by Wolfgang Skala, isn't it? Commented Jan 1, 2014 at 20:59
  • 7
    For the ill-informed (like me), this provides a nice illustration of the accepted answer.
    – Joost
    Commented Sep 29, 2014 at 7:43
9

Hmmm, doesn't look like there are any ways to do this in the package directly. It's relatively easy to make it possible though. It does require modifying the package and to be honest I haven't tested the effect it has on linking, grouping, progress, etc. Although if there are problems they can be fixed in a similar manner. First we need to add an option key to affect linespacing. At the beginning of the sty file you will see a lot of keydefs, we add ours here. Lets call the key linespacing.

\@gtt@keydef{linespacing}

Then we add it to ganttset to ensure it's set to 1 by default. Just add linespacing=1, in the list. Now for the actual modifications we go to the ganttbar command. Here we have definitions for y@upper and y@lower these determine the dimensions of the bars. We modify them as follows:

\def\y@upper{\@gtt@get{linespacing}*\value{gtt@currentline}%
+\value{gtt@lasttitleline}-\@gtt@get{linespacing}*\value{gtt@lasttitleline}%
-\@gtt@get{bar top shift}}%
\def\y@lower{\@gtt@get{linespacing}*\value{gtt@currentline}%
+\value{gtt@lasttitleline}-\@gtt@get{linespacing}*\value{gtt@lasttitleline}%
-\@gtt@get{bar top shift}-\@gtt@get{bar height}}%

All we do is scale the newlines that are not in the title by the factor we assigned to linespacing.

You should note that changing a package in this way, especially without checking all the options like I have done here, is not recommended. It can give you a quick way to obtain what you want though. You should never modify the original package, make a copy, place it in your local tex tree and modify that, call it mypgfgantt.sty and include that instead.

Now you can create a picture with less linespacing as follows:

\documentclass[a4paper,11pt]{article}
\usepackage{mypgfgantt}
\begin{document}
 \begin{tikzpicture}
  \begin{ganttchart}[bar={fill=red!50}]{5}
   \gantttitle{2012--2017}{5} \\
   \gantttitlelist{1,...,5}{1} \\
   \ganttbar{PhD1}{1}{3.5} \\
   \ganttbar{PhD2}{2.5}{5} \\
  \end{ganttchart}
 \end{tikzpicture}
 %
 \begin{tikzpicture}
  \begin{ganttchart}[bar={fill=red!50},linespacing=0.5]{5}
   \gantttitle{2012--2017}{5} \\
   \gantttitlelist{1,...,5}{1} \\
   \ganttbar{PhD1}{1}{3.5} \\
   \ganttbar{PhD2}{2.5}{5} \\
  \end{ganttchart}
 \end{tikzpicture}
\end{document}

enter image description here

This is my first posting here, hope it's up to standards and this (kind of) solves your problem.

3
  • I uploaded the image for you. Welcome to TeX.SX! Commented Oct 4, 2011 at 12:00
  • Thanks. I have emailed the package author and will let you know if he replies.
    – Simd
    Commented Oct 4, 2011 at 16:37
  • I got a reply. He will look into it :)
    – Simd
    Commented Oct 7, 2011 at 12:59

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .