2

I am trying to create a \newcommand to save space. I won't get into the debates as to whether new commands are good or bad. I just know that my code is far easier for ME to debug with my custom commands. Some of my equations are ridiculously long and having them run over multiple lines is a mess. I figure if I have to "find and replace" them later it won't be a huge deal.

With that out of the way...here is an MWE. This works:

\documentclass[class=article, crop=false]{standalone}

\usepackage{tikz}
\usepackage[landscape, margin=0.5in]{geometry}
\usepackage{pgfgantt}

\newcommand{\ct}{blue}  % Color Theme
\newcommand{\tc}{\Large\bfseries\color{white}} % Title Text Format

\begin{document}
%%
\begin{tikzpicture}[font=\sffamily]

%%%
\begin{ganttchart}
    [
    expand chart=\textwidth,
    time slot format=isodate,
    title/.append style={fill=\ct!50},
    title label font = \tc,
    bar/.append style={fill=\ct!10},
    bar height = 0.3,
    vgrid = {   *3{draw=none}, {dashed},         % Jan
                *6{draw=none}, {dashed}, 
                *6{draw=none}, {dashed}, 
                *6{draw=none}, {dashed}, 
                *5{draw=none}, {line width=1pt}, *1{dashed},  % Feb
                *6{draw=none}, {dashed}, 
                *6{draw=none}, {dashed}, 
                *6{draw=none}, {dashed}, 
                *6{draw=none}, {line width=1pt}
                },
    ]
    {2020-01-01}{2020-02-29}

\gantttitle{Project Timeline}{60} \\ % title 1
\gantttitlecalendar*{2020-01-01}{2020-02-29}{month=name} \\

\end{ganttchart}

\end{tikzpicture}

\end{document}

If I define the following new commands so that I can easily change the style of the Gantt chart:

\newcommand{\dn}[1]{*#1{draw=none}} % Shorthand for draw=none
\newcommand*{\ws}{dashed}  % Style for weekly vertical lines
\newcommand*{\ms}{line width=1pt}    % Style for Monthly vertical lines

And replace the vgrid command with:

vgrid = {   \dn{3}, {\ws},         % Jan
            \dn{6}, {\ws}, 
            \dn{6}, {\ws}, 
            \dn{6}, {\ws}, 
            \dn{6}, {\ms}, *1{\ws},  % Feb
            \dn{6}, {\ws}, 
            \dn{6}, {\ws}, 
            \dn{6}, {\ws}, 
            \dn{6}, {\ms}   % Mar
    }

It does not work. I get errors about the command 'tikz/'*3{draw=none}' and similar for the other two commands.

I have searched and searched and can't seem to figure out why these don't work. I literally just want to be able to change the style of my lines with ONE change instead of a bunch (this is two months of the 8 month Gantt chart, so, we're talking about a lot of find and replace to switch from line width=1pt to linewidth=2pt for example.

4
  • Oh, and if anyone knows a simpler way to put in the vertical lines at weeks and months, I would LOVE to hear it. I did a lot of searching for that as well and this is apparently the "only" way it works? When I feel like "draw a vertical line at the end of each month" should have a command of some kind?
    – Jennifer
    Dec 3, 2019 at 18:41
  • Welcome! I guess all the \dn(3) need to become \dn{3}. Also I do not think you need/should put the ganttchart in a tikzpicture environment. Most importantly, what you want to do is IMHO much more conveniently done with styles.
    – user194703
    Dec 3, 2019 at 18:44
  • Ooops, I tried it with \dn{3} at first. I switched to the \def version and never swapped those back. Get the same error with \dn{3}. :D I'll fix that in the original question (since it was just a copy/paste error).
    – Jennifer
    Dec 3, 2019 at 18:52
  • I added an answer that fixes the errors. You need to add /.expanded to expand your macros such that the gantt chart knows these are "theirs".
    – user194703
    Dec 3, 2019 at 18:55

1 Answer 1

1

Welcome! To get rid of the errors, I replaced the parentheses in \dn(<integer>) by braces, and added the /.expanded key. This is necessary to make sure pgffgantt "knows" it does not have look elsewhere to parse these keys.

\documentclass[class=article, crop=false]{standalone}

\usepackage[landscape, margin=0.5in]{geometry}
\usepackage{pgfgantt}

\newcommand{\ct}{blue}  % Color Theme
\newcommand{\tc}{\Large\bfseries\color{white}} % Title Text Format
\newcommand{\dn}[1]{*#1{draw=none}} % Shorthand for draw=none
\newcommand*{\ws}{dashed}  % Style for weekly vertical lines
\newcommand*{\ms}{line width=1pt} 
\begin{document}
%%

%%%
\begin{ganttchart}
    [
    expand chart=\textwidth,
    time slot format=isodate,
    title/.append style={fill=\ct!50},
    title label font = \tc,
    bar/.append style={fill=\ct!10},
    bar height = 0.3,
    vgrid/.expanded= {   \dn{3}, {\ws},         % Jan
            \dn{6}, {\ws}, 
            \dn{6}, {\ws}, 
            \dn{6}, {\ws}, 
            \dn{5}, {\ms}, *1{\ws},  % Feb
            \dn{6}, {\ws}, 
            \dn{6}, {\ws}, 
            \dn{6}, {\ws}, 
            \dn{5}, {\ms}   % Mar
    },
    ]
    {2020-01-01}{2020-02-29}

\gantttitle{Project Timeline}{60} \\ % title 1
\gantttitlecalendar*{2020-01-01}{2020-02-29}{month=name} \\

\end{ganttchart}

\end{document}

enter image description here

I also removed the tikzpicture environment. The result is too wide, as you probably know. This can be fixed by replacing the preamble by

\documentclass[varwidth]{standalone}
\usepackage{pgfgantt}

However, I feel that this is a bit of an XY question, i.e. if you explain what you want to achieve there might be a more elegant solution with styles.

3
  • /.expanded....interesting. I am usually pretty good at reading the package documents and figuring out what I need to do, but have never heard of /.expanded. Where would I find that?
    – Jennifer
    Dec 3, 2019 at 19:02
  • And yes, sorry....potentially more elegant would be lovely. Basically instead of having a vertical grid EVERY SINGLE TIME PERIOD (1 day), I wanted to mark out the weeks and months. So the dashed lines are weeks, the solid lines are months. I'm not set on dashed and solid, but the whole point of my commands was to be able to easily change that style. But the entire point is to have one type of line mark the weeks, and one mark the months.
    – Jennifer
    Dec 3, 2019 at 19:04
  • @Jennifer /.expanded is explained in section 87.4.6 Expanded and Multiple Values of the pgfmanual. I think your approach is fine (will be offline now for a while).
    – user194703
    Dec 3, 2019 at 19:12

You must log in to answer this question.

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