![Tikz \newcommand 错误](https://linux22.com/image/478935/Tikz%20%5Cnewcommand%20%E9%94%99%E8%AF%AF.png)
我正在尝试创建一个 \newcommand 来节省空间。我不会参与关于新命令是好是坏的争论。我只知道使用自定义命令调试代码对我来说要容易得多。我的一些方程式长得离谱,如果让它们运行在多行上,那就太乱了。我想如果我以后必须“查找并替换”它们,那也没什么大不了的。
解决了这个问题...下面是 MWE。它有效:
\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}
如果我定义以下新命令,这样我就可以轻松更改甘特图的样式:
\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
并将 vgrid 命令替换为:
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
}
它不起作用。我收到有关命令 'tikz/'*3{draw=none}' 的错误,其他两个命令也出现类似错误。
我搜索了又搜索,但似乎无法弄清楚为什么这些不起作用。我实际上只想通过一次更改而不是多次更改来更改线条的样式(这是 8 个月甘特图中的两个月,因此,我们谈论的是大量查找和替换,例如从线宽 = 1pt 切换到线宽 = 2pt。
答案1
欢迎!为了消除错误,我\dn(<integer>)
用花括号替换了括号,并添加了/.expanded
键。这是必要的,以确保pgffgantt
“知道”它不必在其他地方解析这些键。
\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}
我还删除了tikzpicture
环境。结果太宽了,你可能知道。这个问题可以通过将前言替换为
\documentclass[varwidth]{standalone}
\usepackage{pgfgantt}
但是,我觉得这有点像 XY 问题,也就是说,如果你解释了你想要实现的目标,那么可能会有一个更优雅的解决方案。