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
subtractand/oranchorwill require replacement of eachsubtractand/oranchoroccurrence in eachmyLinestyle which has parameters passed to it (and there may be tens or hundreds, if not more, in the entire document): compare to renaming\subtractand/or\anchornewcommands which are used ONLY inside\myLinenewcommand(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}]).


\myLhas one optional parameter, and can be called by\myLor\myL[south]. How does the other optional parameter fit in? How do you intend to call\myL? – Teepeemm May 12 '19 at 04:29\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) – user187802 May 12 '19 at 04:40pgf, 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. – Joseph Wright May 14 '19 at 14:59/utils/exec = \ExplSyntaxOn \some_expl_code:\ExplSyntaxOff, which will fail as tokenization has already occurred. – Joseph Wright May 14 '19 at 17:17/utils/exec={\ExplSyntaxOn SOME_EXPL_CODE \ExplSyntaxOff}. As soon asSOME_EXPL_CODEremains without on/off switches (which are placed outside), it works. I wonder why? – bp2017 May 14 '19 at 17:29\ExplSyntaxOn\pgfkeys{/utils/exec = SOME_EXPL_CODE}\ExplSyntaxOff. – Joseph Wright May 14 '19 at 17:32