1

I've been working with this gantt chart example here. I would like to add user specific colore bars. Essentially, I don't want to have to type bar/.append style={fill=green} into every new bar command. I would like to just pass a simple command argument to \ganttbar. As an example

\documentclass[border=10pt]{standalone}%{article}
%\usepackage[margin=1cm]{geometry}
\usepackage{pgfgantt}
\usepackage{graphicx}
\usepackage{xcolor}
%\usetikzlibrary{positioning}

\ganttset{group/.append style={orange},
milestone/.append style={red},
progress label node anchor/.append style={text=red}}

\newcommand{\USERtask}{bar/.append style={fill=green}}

\begin{document}

%    \begin{figure}
%    \centering
     \begin{ganttchart}[%Specs
     y unit title=0.5cm,
     y unit chart=0.7cm,
     vgrid,hgrid,
     title height=1,
%     title/.style={fill=none},
     title label font=\bfseries\footnotesize,
     bar/.style={fill=blue},
     bar height=0.7,
%   progress label text={},
     group right shift=0,
     group top shift=0.7,
     group height=.3,
     group peaks width={0.2},
     inline]{1}{24}
    %labels
    \gantttitle{A two-years project}{24}\\  % title 1
    \gantttitle[]{2013}{12}                 % title 2
    \gantttitle[]{2014}{12} \\              
    \gantttitle{Q1}{3}                      % title 3
    \gantttitle{Q2}{3}
    \gantttitle{Q3}{3}
    \gantttitle{Q4}{3}
    \gantttitle{Q1}{3}
    \gantttitle{Q2}{3}
    \gantttitle{Q3}{3} 
    \gantttitle{Q4}{3}\\
    % Setting group if any
    \ganttgroup[inline=false]{Group 1}{1}{5}\\ 
    \ganttbar[progress=10,inline=false]{Planning}{1}{4}\\
    \ganttmilestone[inline=false]{Milestone 1}{9} \\

    \ganttgroup[inline=false]{Group 2}{6}{12} \\
    %
    % QUESTION IS HERE
    %\ganttbar[progress=20,inline=false]{test1}{10}{19} \\ 
    \ganttbar[progress=20,inline=false,\USERtask]{test1}{10}{19} \\
    %
    %
    \ganttmilestone[inline=false]{Milestone 2}{17} \\
    \ganttbar[progress=5,inline=false]{test2}{11}{20} \\
    \ganttmilestone[inline=false]{Milestone 3}{22} \\       

    \ganttgroup[inline=false]{Group 3}{13}{24} \\ 
    \ganttbar[progress=90,inline=false]{Task A}{13}{15} \\ 
    \ganttbar[progress=50,inline=false, bar progress label node/.append style={below left= 10pt and 7pt}]{Task B}{13}{24} \\ \\
    \ganttbar[progress=30,inline=false]{Task C}{15}{16}\\ 
    \ganttbar[progress=70,inline=false]{Task D}{18}{20} \\ 
\end{ganttchart}
%    \caption{Gantt diagram for 2013--2014 Project}
%\end{figure}
\end{document}

where I tried to create a new command

\newcommand{\USERtask}{bar/.append style={fill=green}}

then within the document gantt environment, I tried to call the command

\ganttbar[progress=20,inline=false,\USERtask]{test1}{10}{19}

but it spits out an error " I do not know the key '/pgfgantt/bar/.append style={fill=green}' and I am going to ignore it."

How can I pass a global argument to \ganttbar[ ] instead of typing \ganttbar[bar/.append style={fill=green}] everywhere?

  • Please provide a complete example i.e. code for a minimal document we can compile to produce that error. – cfr Mar 19 '17 at 0:16
  • I gave a link to the complete example. You just have to add in the new command, then call it as an argument. In the first sentence "here" – ThatsRightJack Mar 19 '17 at 0:18
  • 1
    That is not a complete example which produces the error when I try to compile it. In any case, it would be better to give it here. Link to attribute it, but keep this question self-contained. Otherwise, it becomes useless if the linked content changes. If somebody adds another answer there, how do we here know which code you used? In any case, a pain to have to try to patch together what you might or might not have done from fragments, which is all we have right now. – cfr Mar 19 '17 at 0:22
  • Better to make a style shortcut so you can say something like user task=green. – cfr Mar 19 '17 at 0:24
  • I added the working example. If you compile it as is, it will spit the error. If you switch the comments below where I marked "% QUESTION IS HERE", it will compile and run – ThatsRightJack Mar 19 '17 at 0:30
2

The easiest way to do this is to use a style rather than a macro. I'm not sure why this isn't an option - you already have a number of styles doing not dissimilar things.

\documentclass[border=10pt]{standalone}
\usepackage{pgfgantt}

\ganttset{
  group/.append style={orange},
  milestone/.append style={red},
  progress label node anchor/.append style={text=red},
  user task/.style={bar/.append style={fill=green}},
}

\newcommand{\USERtask}{bar/.append style={fill=green}}

\begin{document}
     \begin{ganttchart}[%Specs
     y unit title=0.5cm,
     y unit chart=0.7cm,
     vgrid,hgrid,
     title height=1,
     title label font=\bfseries\footnotesize,
     bar/.style={fill=blue},
     bar height=0.7,
     group right shift=0,
     group top shift=0.7,
     group height=.3,
     group peaks width={0.2},
     inline]{1}{24}
    %labels
    \gantttitle{A two-years project}{24}\\  % title 1
    \gantttitle[]{2013}{12}                 % title 2
    \gantttitle[]{2014}{12} \\
    \gantttitle{Q1}{3}                      % title 3
    \gantttitle{Q2}{3}
    \gantttitle{Q3}{3}
    \gantttitle{Q4}{3}
    \gantttitle{Q1}{3}
    \gantttitle{Q2}{3}
    \gantttitle{Q3}{3}
    \gantttitle{Q4}{3}\\
    % Setting group if any
    \ganttgroup[inline=false]{Group 1}{1}{5}\\
    \ganttbar[progress=10,inline=false]{Planning}{1}{4}\\
    \ganttmilestone[inline=false]{Milestone 1}{9} \\

    \ganttgroup[inline=false]{Group 2}{6}{12} \\
    %
    % QUESTION IS HERE
    %\ganttbar[progress=20,inline=false]{test1}{10}{19} \\
    \ganttbar[progress=20,inline=false,user task]{test1}{10}{19} \\
    %
    %
    \ganttmilestone[inline=false]{Milestone 2}{17} \\
    \ganttbar[progress=5,inline=false]{test2}{11}{20} \\
    \ganttmilestone[inline=false]{Milestone 3}{22} \\

    \ganttgroup[inline=false]{Group 3}{13}{24} \\
    \ganttbar[progress=90,inline=false]{Task A}{13}{15} \\
    \ganttbar[progress=50,inline=false, bar progress label node/.append style={below left= 10pt and 7pt}]{Task B}{13}{24} \\ \\
    \ganttbar[progress=30,inline=false]{Task C}{15}{16}\\
    \ganttbar[progress=70,inline=false]{Task D}{18}{20} \\
\end{ganttchart}
\end{document}

green bar

If you want to be able to specify the colour, rather than it always being green, you can use

user task/.style={bar/.append style={fill=#1}},

and then say

user task=green

to do the same thing, which is obviously a bit more flexible and probably closer to what you are really trying to do.

  • Yeah, that's what I was trying to do. Didn't know that was an option. – ThatsRightJack Mar 19 '17 at 0:48
  • Should I change the title of the question...your solution solved my problem, but doesn't technically answer the question. – ThatsRightJack Mar 19 '17 at 0:56
  • It is up to you. I took it to be an XY problem i.e. you were asking how to X because you wanted to Y and thought X the best way to Y. However, Xing is actually quite hard here, whereas Zing is easy and you can Y by Zing instead. But it is your question. What I'd recommend is changing the question so you ask how to do what needed to be done, rather than how to implement a solution in a particularly awkward and tricky way. (Anything TikZ means expansion nightmares, best avoided when at all possible!) – cfr Mar 19 '17 at 1:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.