[Tex/LaTex] TikZ: Omit {} after \node declaration for unlabelled node

nodestikz-pgf

This may seem a bit trivial, but I am writing TikZ code with an enormous number of unlabelled nodes. It frustrates me that I have to type {} at the end of every node declaration. Is there some straightforward way to avoid having to type this? Omitting the {} gives a compilation error: A node must have a (possibly empty) label text.

Best Answer

The node contents option can be used instead of the curley braces.

You can apply it to all nodes:

\documentclass{article}

\usepackage{tikz}

\tikzset{
    every node/.append style = {
        node contents =,
    },
}

\begin{document}
    \begin{tikzpicture}
        \node[fill=red];
    \end{tikzpicture}
\end{document}

Or you can define a custom style that you apply to all nodes in question:

\documentclass{article}

\usepackage{tikz}

\tikzset{
    notext/.style = {
        node contents =,
    },
}

\begin{document}
    \begin{tikzpicture}
        \node[notext, fill=red];
    \end{tikzpicture}
\end{document}

See TikZ documentation page 215.

Please note that "When the option [node contents] is used inside the options of a node, the parsing of the node stops immediately after the end of the option block."

But it is still possible to name a node with the name option.

Related Question