When \myCoordinate
accepts MANDATORY argument, following works:
\documentclass{standalone}
\usepackage{tikz}
\newcommand\myCoordinate[1]{#1}
\newcommand\myPath[1][(1,1)]{\path[draw=red]\myCoordinate{#1}--(1,0);}
\begin{document}
\begin{tikzpicture}
\myPath
\end{tikzpicture}
\end{document}
But not when \myCoordinate
accepts OPTIONAL argument (replace \myCoordinate
and \myPath
definitions from above with the following code):
% modify \myCoordinate to accept optional argument and set default value for that argument to (0,0)
\newcommand\myCoordinate[1][(0,0)]{#1}
% replace curly braces (for mandatory argument) with square brackets (for optional argument)
\newcommand\myPath[1][(1,1)]{\path[draw=red]\myCoordinate[#1]--(1,0);}
% you can replace \myPath definition with following one so that optional argument of \myCoordinate reverts to default (but it still doesn't work)
%\newcommand\myPath[1][(1,1)]{\path[draw=red]\myCoordinate--(1,0);}
Judging by the error generated (Giving up on this path. Did you forget a semicolon?), tikz
is complaining because something is wrong with expansion of the macro. And that expansion problem is caused by the optional argument of \myCoordinate
.
I need \myCoordinate
to work while accepting single argument, which is optional argument. I need to make this work with as less complicated code as possible.
On a sidenote, to learn how to send values only to specific optional arguments out of all optional arguments that a command accepts, see this.
Another approach fails alike.
When (instead of having \myCoordinate
inside \myPath
) \myCoordinate
is passed to \myPath
as an argument instead, it fails again when the argument of \myCoordinate
is optional:
\documentclass{standalone}
\usepackage{tikz}
% following \myCoordinate accepts optional argument and fails when passed as an argument to \myPath
\newcommand\myCoordinate[1][0,0]{#1}
\newcommand\myPath[1][(1,1)]{\path[draw=red]{#1}--(1,0);}
\begin{document}
\begin{tikzpicture}
% neither of the two following lines work when \myCoordinate accepts optional argument
%\myPath[\myCoordinate] % fails
%\myPath[\myCoordinate[(0,0)]] % fails
\end{tikzpicture}
\end{document}
But when definition of \myCoordinate
from above and the use of \myPath
from above are changed to the following, it works again:
\newcommand\myCoordinate[1]{#1} % works
\myPath % works
%\myPath[\myCoordinate{(0,0)}] % works
So the optional-argument version of \myCoordinate
can neither be used directly inside \myPath
nor its value can be passed to \myPath
as an argument.
My comment on the answer provided by @marmot.
There is considerable downside of passing (pgf) key-value pairs as arguments to a path's style compared to newcommand
or NewDocumentCommand
(at least in the form showed in your answer, not sure if it can be corrected):
- too long: compare
\myLine[1][north]
to\path[myLine={subtract=1,anchor=north}]
, it's like referring to 3 separate commands at once (\myLine
,\subtract
,\anchor
, or\myLine[\subtract[1]][\anchor[north]]
) - because of this, renaming (if need be) of
subtract
and/oranchor
will require replacement of eachsubtract
and/oranchor
occurrence in eachmyLine
style which has parameters passed to it (and there may be tens or hundreds, if not more, in the entire document): compare to renaming\subtract
and/or\anchor
newcommand
s which are used ONLY inside\myLine
newcommand
(in the document preamble)
If possible, solution should be (using value, not key-value, arguments---which relies on position or order of the values passed as arguments, not on their keys) the following: modify tikzset
code to replace \path[myLine={subtract=1,anchor=north}]
with something like \path[myLine={1,north}]
where each of the arguments is optional (meaning that all of the following are valid and work as expected \path[myLine]
, \path[myLine={1}]
, and \path[myLine={north}]
).
\myL
has one optional parameter, and can be called by\myL
or\myL[south]
. How does the other optional parameter fit in? How do you intend to call\myL
?\path[draw=red](\myN.#1 west)--(\myN.#1 east);
makes no sense here because it doesn't use the second optional parameter #2 (default north)pgf
, and I'm really not sure this is the best way to learn low-level stuff. Certainly I'd stick topgf
's own tools here.\ExplSyntaxOn\pgfkeys{/utils/exec = SOME_EXPL_CODE}\ExplSyntaxOff
.