# How do I make a parameter optional for a Tikz library?

I am making a tikz library and I would like to use an optional parameter. So far, I have the following code

The tikzlibraryquestion.code.tex file is

\usepackage{question}

\tikzset{
question/.pic={
\question@draw
},
}


There is a question.sty file

\ProvidesPackage{question}

\RequirePackage{tikz}

\newcommand*{\mycircle}[1][]{
\tikzset{%
/mycircle/.cd,#1}\mycircle@draw%
}

\tikzset{
/mycircle/.cd,
/mycircle/tilt/.code = 0    \def\mycircle@tilt{#1},
}

\def\mycircle@draw{
\draw (0, 0) circle (1);
\begin{scope}[rotate=\mycircle@tilt]
\draw (0, 0) -- (0, 1);
\end{scope}
}


Finally, the example of use

\documentclass{standalone}

\usepackage{tikz}
\usetikzlibrary{question}

\begin{document}
\begin{tikzpicture}
\mycircle[tilt=23]
\end{tikzpicture}

\begin{tikzpicture}
\mycircle
\end{tikzpicture}
\end{document}


How do I change the .sty file so that the tilt parameter be optional ?

• Would you be OK with a pic instead of a macro? This would be so much easier and more natural.
– user194703
Mar 8, 2020 at 22:06
• @Schrödinger'scat I guess ? I hadn't heard of pics before Mar 8, 2020 at 22:16

You could just use a pic. pics are made for this.

\documentclass{standalone}
% you can put the following three lines in a package, of course
\usepackage{tikz}
\tikzset{pics/my circle/.style={code={
\draw (0,1) -- (0, 0) circle [radius=1];}}}
\begin{document}
\begin{tikzpicture}
\pic[rotate=23]{my circle};
\end{tikzpicture}

\begin{tikzpicture}
\pic{my circle};
\end{tikzpicture}

\begin{tikzpicture}
\pic[red,dashed,scale=2,rotate=-33]{my circle};
\end{tikzpicture}
\end{document}


As you can see, you can add as many optional parameters as you wish.

For more advanced options, use pgf keys. Example:

\documentclass{standalone}
% you can put the following three lines in a package, of course
\usepackage{tikz}
\tikzset{pics/my circle/.style={code={%
\tikzset{my circle/.cd,#1}%
\def\pv##1{\pgfkeysvalueof{/tikz/my circle/##1}}%
\draw[pic actions,my circle/line] (0,1) -- (0, 0);
\draw[pic actions,my circle/circle] (0,0) circle [radius=1];}},
my circle/.cd,line/.style={},circle/.style={}}
\begin{document}
\begin{tikzpicture}
\pic[rotate=23]{my circle};
\end{tikzpicture}

\begin{tikzpicture}
\pic{my circle={line/.style={dashed,thick,blue}}};
\end{tikzpicture}

\begin{tikzpicture}
\pic[rotate=123,red] {my circle={line/.style={dashed,thick,blue}}};
\end{tikzpicture}

\begin{tikzpicture}
\pic[red,dashed,scale=2,rotate=-33]{my circle};
\end{tikzpicture}
\end{document}


• How would I go about if I want the line style to be different for the circle and for the line? Mar 20, 2020 at 18:15