1

Code:

\documentclass[12pt]{article}
\usepackage{pgfplots}
\usepackage{float}
\pgfplotsset{compat=1.17}
\usepackage{tikz}
\usetikzlibrary{shapes, arrows.meta, automata, positioning, matrix, calc}
\usepackage[RPvoltages, american,siunitx]{circuitikz}
\usepackage[margin=1in]{geometry} 
\begin{document}    
    \begin{figure}[H]\centering
        \begin{tikzpicture} [declare function = {buffHeight = 5; vertDis = 2;}] 
            \def\buffHeight{5};
            \def\vertDis{2};
            \draw(0,0) node[draw, minimum width = 1.25cm, minimum height = \buffHeight] (Samp1){}; 
            \draw(\vertDis,0) node[draw, minimum width = 1.25cm, minimum height = \buffHeight] (Samp2){};
            \draw(0,-6) node[draw, minimum width = 1.25cm, minimum height = buffHeight] (Samp3){};
            \draw(vertDis,-6) node[draw, minimum width = 1.25cm, minimum height = buffHeight] (Samp4){};
        \end{tikzpicture} 
    \end{figure}
\end{document}

Result:

enter image description here

\vertDis and vertDis gave me the same results as well as \buffHeight and buffHeight. But when I put in cm for the macro and the variable version i.e. \vertDis cm and vertDis cm, the former worked, while the latter gave me an error. Why does the macro version work with cm, but not the non-macro version?

Superman
  • 1,615
  • you need braces otherwise tikz cannot make out the start and end points – js bibra Oct 11 '20 at 06:32
  • So like {vertDis} cm? – Superman Oct 11 '20 at 06:37
  • I actually tried that, but it didn’t work. Can you show me how it’s actually done? – Superman Oct 11 '20 at 06:39
  • 1
    I wonder if vertDis*1cm works. – Symbol 1 Oct 11 '20 at 06:41
  • That worked! Interesting, I wonder why. – Superman Oct 11 '20 at 06:42
  • 2
    So keys like minimum height will throw its argument into \pgfmathsetlength or a similar macro (so that users can input 1cm + 1in*3). But mixing functions and units confuses it. So better separate them by operators. (BTW, \vertDis will be faster than vertDis; so I always stick to the former.) – Symbol 1 Oct 11 '20 at 06:50
  • Why is \vertDis faster than vertDis? Also, besides minimum height, what about the coordinates like \vertDis? – Superman Oct 11 '20 at 06:52
  • 2
    The general idea is that macro-constants, like \def\a{1.41421} and \pgfmathsetmacro\b{sqrt(2)} are expanded by TeX itself and is fast. On the other hand, a nullary function will undergo PGF's math engine, which is some heavy string-parsing stuff. For coordinates, such as (2, 3), they undergo another family of macros (\tikz@scan@one@point etc). A way to distinguish them is that, while the default unit for line width is pt; the default unit of coordinates is cm. – Symbol 1 Oct 11 '20 at 07:00
  • Interesting, what do you mean by “nullary” by the way. Can you clarify that adjective? – Superman Oct 11 '20 at 07:04
  • 1
    A nullary (0-ary) function is a function that takes 0 arguments. In (at least) mathematical logic, constants are treated as nullary functions to unify the terminology. – Symbol 1 Oct 11 '20 at 07:13
  • 1
    if you don't provide a unit for tikz arguments like height and width, you may get lucky or you may not. I try not to rely on luck. – John Kormylo Oct 11 '20 at 18:31

0 Answers0