TransWikia.com

How to use curly brackets as part of an output of a command?

TeX - LaTeX Asked by 607 on December 3, 2020

There’s an exercise that I am clearly intended to do on paper, but for some reason I insist on doing it in LaTeX. It takes a while, though, and as such I would like to make one or more commands to abbreviate some things. In general I think this would be useful to be able to do.

I get stuck quickly, though. I have in a tikzpicture environment the following:
node (square) {textit{square}} ;, which produces the desired result. When I then create a command newcommand{testa}[1]{ textit{#1} } and use node (square) {testa{square}} ;, it still works. When I create a command newcommand{testb}[1]{ { textit{#1} } } and use node (square) testb{square} ; however, I get "Package tikz Error: A node must have a (possibly empty) label text.".
How can this be, and can I fix it? If so, how?
Thanks. 🙂 If I know how to fix this, I can expand the command so that node testc{square} will produce the desired result.

One Answer

You can do it with a little bit of expansion trickery but that is probably not what you are looking for because it is even more verbose than writing it out explicitly.

documentclass{article}
usepackage{tikz}
newcommand{testb}[1]{{textit{#1}}}
begin{document}
begin{tikzpicture}
  expanded{noexpandnode (square) unexpandedexpandafter{testb{square}};}
end{tikzpicture}
end{document}

You can also give this poorly tested patch to the internal TikZ code a shot. It introduces quite a lot of overhead in the expansion chain and produces errors that are difficult to make sense of and possibly hard to recover.

documentclass{article}
usepackage{tikz}

makeatletter
lettikz@@scan@fig@normaltikz@@scan@fig
deftikz@@scan@fig{%
  pgfutil@ifnextcharspace
    {errmessage{Can't happen!}}%
    {expandaftertikz@@scan@fig@normalromannumeral-`0}}
makeatother

newcommand{testb}[1]{ {textit{#1}} }
newcommand{testc}[1]{ (#1) {textit{#1}} }

begin{document}
begin{tikzpicture}
  node (square) testb{square};

  node[draw] at (2,0) testc{rectangle};
end{tikzpicture}
end{document}

Answered by Henri Menke on December 3, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP