5

I am trying to write a formal proof of validity that uses conditional proof (C.P.) arrows. I came across gernot's code for them at https://tex.stackexchange.com/a/581822/277990, and below I have applied their idea to a different example. My question is, how does the syntax of gernot's tikznode command work? In other words, what is the role of each of the parts of [3][]{%\tikz[remember picture,baseline=(#2.base)]\node[minimum size=0pt,inner sep=0pt,#1](#2){#3};%}? In particular I cannot find anything on the use of hashtags in the TikZ manual at https://mirrors.concertpass.com/tex-archive/graphics/pgf/base/doc/pgfmanual.pdf. And why the different kinds of closing devices (i.e., "( )" vs. "{ }" vs. "[ ]")?

\documentclass{article}
\usepackage{amssymb, mathtools}
\usepackage{tikz}
\usetikzlibrary{calc, arrows}

\let\impl\supset
\renewcommand{\lor}{\text{ v }}

\newcommand\tikznode[3][]{%
    \tikz[remember picture,baseline=(#2.base)]
      \node[minimum size=0pt,inner sep=0pt,#1](#2){#3};%
}

\begin{document}

\(\begin{array}{rll}
                1.      &   (x) \{ (Bx \lor Wx) \impl [(Ax \lor Fx) \impl Sx]\}\mathrlap{\quad/\therefore (x)[Bx \impl (Ax \impl Sx)]}\\
\tikznode{2}  {$2.$}    &   By\\
                3.      &   Ay\\
                4.      &   (By \lor Wy) \impl [(Ay \lor Fy) \impl Sy]\qquad\qquad % This \qquad\qquad is a cheat in order to push the "evidence" column to the right somewhat.
                        &   \text{1, \textbf{UI}}\\
                5.      &   By \lor Wy
                        &   \text{2, Add.}\\
                6.      &   (Ay \lor Fy) \impl Sy
                        &   \text{4, 5, M.P.}\\
                7.      &   Ay \lor Fy
                        &   \text{3, Add.}\\
                8.      &   Sy
                        &   \text{6, 7, M.P.}\\
\tikznode{9}  {$9.$}    &   Ay \impl Sy
                        &   \text{3--8, C.P.}\\
\tikznode{10} {$10.$}   &   By \impl (Ay \impl Sy)
                        &   \text{2--9, C.P.}\\
                11.     &   (x)[Bx \impl (Ax \impl Sx)]
                        &   \text{10, \textbf{UG}}
\end{array}
\)

\begin{tikzpicture}[remember picture,overlay]
  \draw[stealth'-,shorten <=2pt] (2) -- ++(-2em,0) |- ($(9)!0.5!(10)$) -- ++(10em,0);
  % "[shorten <=<length>]" controls shaft of part of arrow right next to "2."]
  % "($(<node no. A>)!0.5!(<node no. B>)$)" specifies the coordinate halfway between node no. A and node no. B
\end{tikzpicture}

\end{document}

Thank you!

1
  • 1
    Just to note that a more sophisticated and flexible version of \tikznode is provided by the tikzmark package as \tikzmarknode. In particular, it automatically detects the ambient math mode. Commented Aug 11, 2022 at 18:51

1 Answer 1

7

Some of the syntax you ask about is standard LaTeX syntax, and some some is specific to TikZ.

How the command works

In its simplest use, the command takes two arguments: a node name, and a node contents, and places the node aligned with the baseline so that it can be used with other text. Furthermore, the nodes are named so that other subsequent TikZ commands can refer to them. This will require two compilations to work properly.

The command also allows one optional argument, which lets you pass other parameters to the node created.

Here's a sample document showing its capabilities.

\documentclass{article}
\usepackage{tikz}
\newcommand\tikznode[3][]{%
    \tikz[remember picture,baseline=(#2.base)]
      \node[minimum size=0pt,inner sep=0pt,#1](#2){#3};%
}
\begin{document}
\tikznode{A}{$A$} some text \tikznode{B}{$B$} some text \tikznode[red]{C}{$C$}
\tikz[remember picture,overlay]{\draw[->,in=-90,out=-90] (A) to (B);}
\end{document}

output of code

The first line creates two nodes A and B, with math content A and B. A third node is also created but also passes red as the optional argument, which then colours the node red.

The second line shows that we can subsequently refer to the created nodes to do things to them. In this case I've drawn a curved arrow between A and B.

As Andrew Stacey notes in the comments, the tikzmark package provides a more general and powerful version of this command, but for many purposes a simple version like this is just fine.

How the command syntax works

The first part of the definition is standard LaTeX \newcommand:

\newcommand[<number of arguments][<optional argument default>]
   {<rest of command  #1 #2 and #3>}

Here the code of the command is delimited by {...}. #1, #2 and #3 refer to the first (in this case optional), second and third arguments respectively. So this command has two obligatory arguments (#2 and #3) and one optional argument (#1).

See \newcommand argument confusion for a more detailed discussion of the syntax.

Now for the TikZ syntax:

\tikz[remember picture,baseline=(#2.base)]
    \node[minimum size=0pt,inner sep=0pt,#1](#2){#3};%

The \tikz command is a way of injecting tikz code directly into a command without needing the tikzpicture environment. TikZ uses square brackets to delimit key/values passed to any TikZ command, parentheses (...) to delimit coordinates and {...} to delimit node content as well as more general ways of grouping elements as in regular TeX.

So this command passes the remember picture key to the \tikz command, which allows elements defined in the picture to be accessed later; it aligns the picture baseline with the base anchor of node named by the second (i.e., first obligatory) argument of the main command.

The actual TikZ command simply places a node using the \node command. Here again, the key/values are passed to that command, setting a minimum size for the node, and removing any inner space between the node content and the outside boundary of the node. The optional argument (if present) (#1 of the newly defined command) is also passed which allows you to pass other TikZ key/values to the node command using this argument. This is how the red key gets passed to the third node in the sample document.

The rest of the command passes the first obligatory argument of the main command (#2) as the name of the node to be created, and passes the second argument (#3) as the node text. Since the whole \tikz command uses remember picture, this name that is created is able to be referred to by other \tikz commands outside of the current one. The final ; is the required TikZ ending character. The comment character % at the end makes sure that no spurious space shows up when the command is used. See What is the use of percent signs (%) at the end of lines? (Why is my macro creating extra space?) for more details on the comment character and spurious spaces.

5
  • Thank you! That's very helpful. Lots of things finally "clicked." In the code of my original post, \tikznode is used three times. The first time, it looks like \tikznode{2}{$2.$}, for example. So would {2} be the first obligatory argument and {$2.$} be the second obligatory argument? If so, then if I understand you right, then it must be giving the name "2" to the node established by this instance of \tikznode; and it must be making "$2.$" into the node text. (And that seems to be what's happening when I mess around with the code.) Have I understood you correctly?
    – Noah J
    Commented Aug 12, 2022 at 0:13
  • Also, I don't think I follow your sentence, "The optional argument (#1 of the newly defined command) is also passed which allows you to pass other TikZ key/values to the node command using this argument."
    – Noah J
    Commented Aug 12, 2022 at 0:17
  • What does the “spurious space” mean? I though % was used to write notes for yourself without having them be put in the document. Commented Aug 12, 2022 at 2:00
  • @NoahJustice I've added some more explanation and an example of the command in action. Hopefully that clears up your question.
    – Alan Munn
    Commented Aug 12, 2022 at 2:57
  • @DevanoBethel I've added a link to a question that explains spurious spaces and the role of the comment character in preventing them.
    – Alan Munn
    Commented Aug 12, 2022 at 2:57

You must log in to answer this question.

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