Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Macros with optional arguments do not work in TikZ commands [Semi-patch] #1147

Closed
ilyaza opened this issue Mar 17, 2022 · 4 comments
Closed

Comments

@ilyaza
Copy link

ilyaza commented Mar 17, 2022

Brief outline of the bug

The TikZ command loop fails handling LaTeX macros with optional arguments. It gives a cryptic error message (like Did you forget to insert a semicolon). (A “semi-patch” included — it works for the older version of the scan loop.)

Minimal working example (MWE)

(The following example includes also a \global\def chunk to illustrate another functionality of the patch.)

\documentclass{article}
\usepackage{tikz}

\begin{document}

\newcommand\foo[1][node {123}]{#1}

\tikz \draw (0,0) node {123} -- ++(1,1);.

\tikz \draw (0,0) \foo \foo[-- ++(1,1)] \global\def\BAR{node {yyy}};.

\tikz \draw (0,0) node {123} -- ++(1,1);.

\tikz \draw \BAR;

\end{document}

How to fix

Right now I cannot test with the latest version. I include the patch for the version in TeXLive2021. I expect the first and the third chunks would work in the bleeding-edge version too (they are only 4 LoCs), and the dispatcher would need an extra chunk like

    {\global}{\let\pgfutil@next\tikz@global}%
    {\let}{\let\pgfutil@next\tikz@assignment}%
    {\futurelet}{\let\pgfutil@next\tikz@assignment}%
    {\def}{\let\pgfutil@next\tikz@assignment}%
    {\gdef}{\let\pgfutil@next\tikz@assignment}%
    {\edef}{\let\pgfutil@next\tikz@assignment}%
    {\xdef}{\let\pgfutil@next\tikz@assignment}%

diff-tikz.code.tex.txt

@hmenke
Copy link
Member

hmenke commented Mar 17, 2022

You cannot use unexpandable material on a path and that is also documented. I'm not going to change that.

@hmenke hmenke closed this as completed Mar 17, 2022
@ilyaza
Copy link
Author

ilyaza commented Mar 17, 2022

I do not follow: with the given patch, you can use this particular kind of unexpandable “material”. So your statement would be valid only in the past tense.

@hmenke
Copy link
Member

hmenke commented Mar 17, 2022

If you want to perform semantic actions (like assignments) on a path use \pgfextra. Macros with unexpandable material are intentionally unsupported, because you will quickly run into problems with incomplete \if or dangling \else.

See also #967

@muzimuzhi
Copy link
Member

A workaround

  • \newcommand\foo[1][...]{...} is replaced with option insert path and key handler /.default,
  • \global\def\BAR{..} is wrapped in \pgfextra
\documentclass{article}
\usepackage{tikz}

\tikzset{
  foo/.style={insert path={#1}},
  foo/.default={node {123}}
}

\begin{document}
\tikz \draw (0,0) node {123} -- ++(1,1);.

\tikz \draw (0,0) [foo] [foo={-- ++(1,1)}]
  \pgfextra{\global\def\BAR{node {yyy}}};.

\tikz \draw (0,0) node {123} -- ++(1,1);.

\tikz \draw \BAR;
\end{document}

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

3 participants