2

When I put a vertical bar "|" in a tikz-cd cell, for example like this:

\documentclass{article}
\usepackage{tikz-cd}
\begin{document}
\begin{tikzcd}
|A|
\end{tikzcd}
\end{document}

then I get the error

 Package tikz Error: A node must have a (possibly empty) label text.

What causes this error? Is this a bug or is it by design?

Placing curly braces around the vertical bars seems to fix this, so by replacing |A| by {|A|}. But it could become tedious when there are a lot of vertical bars. Is there a better workaround?

5
  • 2
    The bar character | is used to escape to tikz options. The expected syntax is |[red]| X_1. To avoid the confusion, write {|X|}.
    – Symbol 1
    Commented Jun 11, 2021 at 23:24
  • 3
    BTW, welcome. And very good minimal working example.
    – Symbol 1
    Commented Jun 11, 2021 at 23:25
  • @Symbol1 Welcome back! I did not see you in a long time. Commented Jun 12, 2021 at 0:48
  • @kiwi. Didn't see that you don't like the idea of {|A|}. Try \makeatletter \let\tikz@lib@matrix@start@cell=\tikz@lib@matrix@normal@start@cell \makeatother. @Dr Was working on my doctoral thesis and will move to UCSD really soon.
    – Symbol 1
    Commented Jun 12, 2021 at 1:08
  • 1
    @Symbol1 Thanks for the reply. I am close to San Francisco (94040 Mountain View). Let me know if you are in the area :). Commented Jun 12, 2021 at 1:13

1 Answer 1

3

Do this

\documentclass{article}
\usepackage{tikz-cd}
\begin{document}
\makeatletter
\let\tikz@lib@matrix@start@cell=\tikz@lib@matrix@normal@start@cell
\makeatother
\begin{tikzcd}
|A|
\end{tikzcd}
\end{document}

Why it works?

TikZ-cd is implemented by a \matrix...; command so everything boils down to how TikZ parses a \matrix. At the beginning of every cell \tikz@lib@matrix@start@cell is executed. It will first check if it sees a vertical bar |.

% tikzlibrarymatrix.code.tex line 28-29
\def\tikz@lib@matrix@start@cell{%
  \pgfutil@ifnextchar|{\tikz@lib@matrix@with@options}%
                      {\tikz@lib@matrix@normal@start@cell}}%

All you need to do is to tell TikZ not to check for |. This can be done by letting start@cell to be normal@start@cell the command that should be executed if there were no bar.

You must log in to answer this question.

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