1

I'm trying to make a tikz drawing using the component lengths as macros so I can easily adjust things and have changes propagate. That works, but when I try to use division to draw lines at even intervals on a macro-defined width, I get problems. I think it is because the fractions are being applied to only the first value in the tableWidth command, as it is just a dumb substitution, rather than combining the values in the command before inserting it into the body.

How can I force latex to evaluate math on a dimension before I apply other operations to it? Are there other packages I should be using to do this? I have googled around and found things relating to dimexpr and others but can't make sense of it or make it work for my context.

\documentclass[letterpaper]{scrartcl}

\usepackage{tikz} \usepackage{graphicx}

\newcommand{\positionRad}{14mm} \newcommand{\positionSpacing}{32mm}

\newcommand{\iconSize}{12mm}

\newcommand{\tableHeight}{20mm} \newcommand{\tablePad}{6mm}

\newcommand{\tablePositionSep}{\positionRad+8mm}

\newcommand{\tableWidth}{4*\positionSpacing + \positionRad}

\graphicspath{{../icons/}}

\begin{document} \begin{tikzpicture}

\draw (\positionSpacing, \tablePositionSep+\tableHeight) circle (\positionRad) node{\LARGE1}; \draw (2\positionSpacing, \tablePositionSep+\tableHeight) circle (\positionRad) node{\LARGE2}; \draw (3\positionSpacing, \tablePositionSep+\tableHeight) circle (\positionRad) node{\LARGE3}; \draw (4*\positionSpacing, \tablePositionSep+\tableHeight) circle (\positionRad) node{\LARGE4};

\draw (\positionSpacing-\positionRad,\tablePad) rectangle (\tableWidth,\tablePad+\tableHeight); \draw (1/6\tableWidth,\tablePad) -- (1/6\tableWidth,\tablePad+\tableHeight); \draw (1/3\tableWidth,\tablePad) -- (1/3\tableWidth,\tablePad+\tableHeight); \draw (1/2\tableWidth,\tablePad) -- (1/2\tableWidth,\tablePad+\tableHeight); \draw (5/6\tableWidth,\tablePad) -- (5/6\tableWidth,\tablePad+\tableHeight); \draw (2/3\tableWidth,\tablePad) -- (2/3\tableWidth,\tablePad+\tableHeight);

\end{tikzpicture}

\end{document}

Treeman
  • 13
  • 1
    \newcommand{\tablePositionSep}{\the\dimexpr\positionRad+8mm} \newcommand{\tableWidth}{\the\dimexpr4*\positionSpacing + \positionRad}? I think that you should remove things of the type \includegraphics[width=\iconSize]{inf.png} because others won't have the png file, and add an appropriate \documentclass so that others can run your file –  May 05 '21 at 00:45

1 Answers1

2

I think it is because the fractions are being applied to only the first value in the tableWidth command, as it is just a dumb substitution, rather than combining the values in the command before inserting it into the body.

Correct, it is dumb substitution.

With

\newcommand\foo{1+2}

the expresion 3*\foo becomes 3*1+2 when PGFmath evaluates it.

You would need 3*(\foo), though, in your case I'd just add the parentheses directly to \foo:

\newcommand\foo{(1+2)}

then 3*\foo (→ 3*(1+2)) is evaluated correctly.

However, since the TikZ parser is greedy when it parses a coordinate, these expressions need to be protected from it by enclosing the components in {…}.

There's also an option to use PGFmath functions defined by declare function, e.g.

declare function={
  tablePositionSep=\positionSpacing+8mm;
  tableWidth=4*\positionSpacing+\positionRad;
}

but mixing that with dimension units can make it messy when used inside TikZ … or I messed something up by converting your picture.

Code

\documentclass[letterpaper]{scrartcl}
\usepackage{tikz}
\usetikzlibrary{backgrounds}
\newcommand{\positionRad}    {14mm}
\newcommand{\positionSpacing}{32mm}
\newcommand{\iconSize}       {12mm}
\newcommand{\tableHeight}    {20mm}
\newcommand{\tablePad}        {6mm}

\newcommand{\tablePositionSep}{(\positionRad+8mm)} \newcommand{\tableWidth} {(4*\positionSpacing + \positionRad)}

\begin{document} \begin{tikzpicture}[radius=\positionRad] \draw (0,0) -- (1,1); \draw ( \positionSpacing, {\tablePositionSep+\tableHeight}) circle [] node{\LARGE1}; \draw (2\positionSpacing, {\tablePositionSep+\tableHeight}) circle [] node{\LARGE2}; \draw (3\positionSpacing, {\tablePositionSep+\tableHeight}) circle [] node{\LARGE3}; \draw (4*\positionSpacing, {\tablePositionSep+\tableHeight}) circle [] node{\LARGE4};

\draw (\positionSpacing-\positionRad, \tablePad) rectangle ({\tableWidth}, \tablePad+\tableHeight); \draw ({1/6\tableWidth}, \tablePad) -- ({1/6\tableWidth}, \tablePad+\tableHeight); \draw ({1/3\tableWidth}, \tablePad) -- ({1/3\tableWidth}, \tablePad+\tableHeight); \draw ({1/2\tableWidth}, \tablePad) -- ({1/2\tableWidth}, \tablePad+\tableHeight); \draw ({5/6\tableWidth}, \tablePad) -- ({5/6\tableWidth}, \tablePad+\tableHeight); \draw ({2/3\tableWidth}, \tablePad) -- ({2/3\tableWidth}, \tablePad+\tableHeight); \end{tikzpicture} \end{document}

Qrrbrbirlbel
  • 119,821