我有一个定义的命令,如MWE所示,如下所示:
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\newcommand\shapes[3]
{\begin{tikzpicture}
\draw[fill=#1] (0,0) rectangle (1,1);
\draw[fill=#2] (0,2.5) circle (0.5);
\draw[fill=#3] (3,2) ellipse (1 and 0.75);
\end{tikzpicture}}
\begin{document}
\shapes{yellow}{green!50}{red!50}
\end{document}
有时我不想给某个特定的形状涂上颜色,这时我将填充颜色设置为none
,如下所示:\shapes{yellow}{none}{none}
。
但是,当我这样做时,我不希望边框保留(换句话说,如果我将颜色指定为,我希望形状完全消失none
- 我不希望边框保留。
有没有一个好的方法可以自动使边框颜色为无(或者可能使命令成为\draw
命令\fill
,如果填充颜色为无(这会消除边框)?
答案1
您可以使用draw
钥匙。
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\newcommand\shapes[3]
{\begin{tikzpicture}
\draw[draw=#1,fill=#1] (0,0) rectangle (1,1);
\draw[draw=#2,fill=#2] (0,2.5) circle (0.5);
\draw[draw=#3,fill=#3] (3,2) ellipse (1 and 0.75);
\end{tikzpicture}}
\begin{document}
\shapes{yellow}{green!50}{red!50}
\shapes{yellow}{none}{none}
\end{document}
更新:此版本保留了边框。
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usepackage{ifthen}
\newcommand\shapes[3]
{\begin{tikzpicture}
\ifthenelse{\equal{none}{#1}}{\def\mycol{#1}}{\def\mycol{black}}
\draw[draw=\mycol,fill=#1] (0,0) rectangle (1,1);
\ifthenelse{\equal{none}{#2}}{\def\mycol{#2}}{\def\mycol{black}}
\draw[draw=\mycol,fill=#2] (0,2.5) circle (0.5);
\ifthenelse{\equal{none}{#3}}{\def\mycol{#3}}{\def\mycol{black}}
\draw[draw=\mycol,fill=#3] (3,2) ellipse (1 and 0.75);
\end{tikzpicture}}
\begin{document}
\shapes{yellow}{green!50}{red!50}
\shapes{yellow}{none}{none}
\end{document}