3

I have a defined command as shown in the MWE as follows:

\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}

Sometimes I don't want to color a particular shape in, which is when I set the fill color to none, like so: \shapes{yellow}{none}{none}.

However, when I do this, I don't want the border to remain (in other words, I want the shape to disappear completely if I specify the color as none – I don't want the border to remain.

Is there a good way to way to automatically make the border color none (or perhaps make the \draw command a \fill command, if the fill color is none (which would get rid of the border)?

3
  • I can't reproduce your problem. Borders are not visible as expected. I wonder if \newcommand\shapes[3] {\begin{tikzpicture} \fill[fill=#1] (0,0) rectangle (1,1); \fill[fill=#2] (0,2.5) circle (0.5); \fill[fill=#3] (3,2) ellipse (1 and 0.75); \end{tikzpicture}} doesn't gives what you like to have?
    – Zarko
    Commented Jun 4, 2017 at 12:20
  • @Zarko Sorry, I don't understand; isn't that code snippet exactly what's in my MWE? Sorry if I wasn't clear – I wanted no borders if the fill was none, and borders otherwise. TeXnician showed me how to do it using ifthen conditionals (which I guess is the best way to do it, it seems) Commented Jun 4, 2017 at 12:29
  • than please correct question ...
    – Zarko
    Commented Jun 4, 2017 at 12:30

1 Answer 1

2

You could use the draw key.

borders

\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}

Update: This version keeps the border.

with border

\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}
4
  • Sorry if I wasn't clear. I want there to be a border if there is a fill color, but none if I specify none as the fill color! Commented Jun 4, 2017 at 12:01
  • @SkeletonBow See the update
    – TeXnician
    Commented Jun 4, 2017 at 12:06
  • I tried updating but it told me I had the latest version of LaTeX Commented Jun 4, 2017 at 12:16
  • 1
    @SkeletonBow If someone writes "See the update" on this sites he/she means the edited post ;)
    – TeXnician
    Commented Jun 4, 2017 at 12:17

You must log in to answer this question.

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