![如果 TikZ 图片中的填充颜色为“无”,则自动隐藏边框](https://linux22.com/image/356179/%E5%A6%82%E6%9E%9C%20TikZ%20%E5%9B%BE%E7%89%87%E4%B8%AD%E7%9A%84%E5%A1%AB%E5%85%85%E9%A2%9C%E8%89%B2%E4%B8%BA%E2%80%9C%E6%97%A0%E2%80%9D%EF%BC%8C%E5%88%99%E8%87%AA%E5%8A%A8%E9%9A%90%E8%97%8F%E8%BE%B9%E6%A1%86.png)
我有一个定义的命令,如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}