3

I want to use a TikZ environment inside an \ExplSyntaxOn... \ExplSyntaxOff block.

\documentclass{article}


\usepackage[utf8]{inputenc}

\usepackage{xparse}

\usepackage{tikz}
\usetikzlibrary{positioning}


\ExplSyntaxOn

\NewDocumentCommand{\maketikz}{ O{,} m }{%
\begin{tikzpicture}[%
    padding/.append style={inner sep=2px},%
    node distance=2pt
]
    \node[draw] {#2};
\end{tikzpicture}
}

\ExplSyntaxOff


\begin{document}
    
\maketikz[;]{Yes}
    
\end{document}

During the build process I get errors for the TikZ styles: I do not know the key '/tikz/padding/.appendstyle', to which you passed 'innersep=2px... and I do not know the key '/tikz/nodedistance', to which you passed '2pt'....

Without \ExplSyntaxOn it builds without any errors.

\documentclass{article}


\usepackage[utf8]{inputenc}

\usepackage{xparse}

\usepackage{tikz}
\usetikzlibrary{positioning}


\newcommand{\maketikz}[2][,]{
\begin{tikzpicture}[%
    padding/.append style={inner sep=2px},%
    node distance=2pt
]
    \node[draw] {#2};
\end{tikzpicture}
}


\begin{document}
    
\maketikz[;]{Yes}
    
\end{document}

Where is my mistake?

5
  • 7
    in expl3 context spaces are ignored, and you must use ~, e.g. padding/.append~style={inner~sep=2px} Commented Aug 3, 2022 at 9:47
  • Don't use px. Very likely it is not what you might think it to be.
    – egreg
    Commented Aug 3, 2022 at 9:52
  • @UlrikeFischer ooohhh, this should have been so obvious, thanks alot!
    – Mountain
    Commented Aug 3, 2022 at 13:24
  • @egreg it was just a typo of CSS muscle memory, thanks
    – Mountain
    Commented Aug 3, 2022 at 13:24
  • @UlrikeFischer Want to make that an answer?
    – schtandard
    Commented Aug 16, 2022 at 8:55

1 Answer 1

0

When the ExplSyntax is turnd On spaces are ignored and ~ needs to be used instead.

To quote the manual interface3.pdf:

The \ExplSyntaxOn function switches to a category code regime in which spaces and new lines are ignored, and in which the colon (:) and underscore (_) are treated as “letters”, thus allowing access to the names of code functions and variables. Within this environment, ~ is used to input a space. The \ExplSyntaxOff reverts to the document category code regime.

Thus you need to use ~ instead of spaces to use the corrent key names.

This also means that you don't have to use % after the opening { or after \end{tikzpicture}.

The ones after [ and , in the options that get passed to PGFKeys aren't necessary either, even without \ExplSyntaxOn because PGFKeys ignores the spaces from the get-go

I've added a bit text around your macro to show that they are no spaces included.

Code

\documentclass{article}
\usepackage{tikz}
\ExplSyntaxOn
\NewDocumentCommand{\maketikz}{ O{,} m }{ % ← space doesn't need to be %ed
\begin{tikzpicture}[
    padding/.append~style = {inner~sep = 2pt},
    node~distance         = 2pt ]
\node[draw, padding] {#2};
\end{tikzpicture} % ← space doesn't need to be %ed
}
\ExplSyntaxOff

\begin{document}
Foo\maketikz[;]{Yes}Bar
\end{document}

Output

enter image description here

You must log in to answer this question.

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