[Tex/LaTex] Pgfgantt: Change anchor for centering

horizontal alignmentpgfgantt

I have a Gantt produced with pgfgantt package. However, when you try to center it centers the entire figure (including names of the tasks).

I would like to have the table centered, and then the names and other things floating relative to it.

I imagine that the tikzpicture has some type of anchor set in the center of the entire set of items to center the image. So, I was wondering if it is possible to shift that anchor to the center of the table, instead of the whole figure.

I went through the manual, but couldn't find information on this regard. I tried to show what I mean by "centering the table" in the figures below. Note that due to the crop I used to produce the image the margins are out, but imagine the margins around the caption of the image.

enter image description here

\documentclass{article}

\usepackage{pgfgantt}

\pagestyle{empty}

\begin{document}
\begin{figure}[h]
  \begin{tikzpicture}
  \begin{ganttchart}{1}{12}
  \ganttbar{Task with really long name}{1}{2} \\
  \end{ganttchart}
  \end{tikzpicture}
  \caption{Not centered on the table.}
\end{figure}

\begin{figure}[h]
  \hspace*{-40px}% manually adjusted
  \begin{tikzpicture}
  \begin{ganttchart}{1}{12}
  \ganttbar{Task with really long name}{1}{2} \\
  \end{ganttchart}
  \end{tikzpicture}
  \caption{Kind of centered on the table.}
\end{figure}

\end{document}

Best Answer

Here's a bit of a roundabout way. Put the whole ganttchart in a pgfinterruptboundingbox environment, which essentially means that it will not be considered when deciding the bounding box. Then add canvas/.append style={alias=frame} to the ganttchart options (see code below), and after the ganttchart environment set the bounding box explicitly with

\useasboundingbox (frame.south west) rectangle (frame.north east);

All of this needs to be inside a tikzpicture environment, though if you just have the ganttchart environment the surrounding tikzpicture is not required.

Also note that you don't want to have \\ after the last ganttbar, as that creates some additional whitespace.

enter image description here

\documentclass{article}

\usepackage{pgfgantt}

\pagestyle{empty}

\begin{document}
\begin{figure}
\centering
\begin{tikzpicture}
\begin{pgfinterruptboundingbox}
  \begin{ganttchart}[canvas/.append style={alias=frame}]{1}{12}
  \ganttbar{Task with really long name}{1}{2} 
  \end{ganttchart}
\end{pgfinterruptboundingbox}
\useasboundingbox (frame.south west) rectangle (frame.north east);
\end{tikzpicture}
  \caption{Not centered on the table.} % actually, it is
\end{figure}

\end{document}
Related Question