2

I would like to create a node with minimum width=αx, where x is the length of the x-coordinate vector specified in the tikzpicture environment, e.g. \begin{tikzpicture}[x=0.1cm,y=2.5ex]. Is this possible?

cxandru
  • 45
  • Welcome to TeX.SX! I'm not sure how to reference that parameter later, but you can use \newcommand\xlen{0.1cm} just before the tikzpicture environment, then start with \begin{tikzpicture}[x=\xlen,y=2.5ex] and use (for example) \node[minimum width=17*\xlen] when defining the node. – Jānis Lazovskis Dec 02 '19 at 10:35

1 Answers1

1

As suggested in this answer, you can define variables to set your x and y scales and a style for your node as in the following example:

\documentclass{article}
\usepackage{tikz}

\newcommand{\unitx}{1cm}
\newcommand{\unity}{1cm}

\tikzset{%
    box/.style 2 args = {%
        rectangle,
        draw,
        minimum width  = #1*\unitx,
        minimum height = #2*\unity
    },
    every tikzpicture/.append style = {
        x = \unitx,
        y = \unity
    }
}

\begin{document}

    \begin{tikzpicture}

        \node[box={2}{3}] at (0, 0) {Hello};

    \end{tikzpicture}

\renewcommand{\unitx}{5cm}
\renewcommand{\unity}{3cm}

    \begin{tikzpicture}

        \node[box={2}{3}] at (0, 0) {Hello};

    \end{tikzpicture}

\end{document}

With this, you impose a given scale on x and y for every tikzpicture environment and you define a style for a rectangle node with minimum width and minimum height based on this scale.

The scales are stored in the \unitx and \unity variables that you can redefine anywhere in your document to change the scale.

If you do not want to apply the scaling globally, remove the every tikzpicture argument of the \tikzset command and apply the scaling locally as in the following example:

\documentclass{standalone}
\usepackage{tikz}

\newcommand{\unitx}{2cm}
\newcommand{\unity}{1cm}

\tikzset{%
    box/.style 2 args = {%
        rectangle,
        draw,
        minimum width  = #1*\unitx,
        minimum height = #2*\unity
    }
}

\begin{document}

    \begin{tikzpicture}[x = \unitx, y = \unity]

        \node[box={2}{3}] at (0, 0) {Hello};

    \end{tikzpicture}

\end{document}
KersouMan
  • 1,850
  • 3
  • 13
  • 15
  • I see that my question is essentially the same as the one you linked. As with the answers to that question, your answer is a workaround, but the nagging question remains: Why do dimensionless node dimensions not use the x/y lengths? – cxandru Dec 02 '19 at 11:33
  • 1
    Ak ok, the answer to (this question)[https://tex.stackexchange.com/questions/50991/use-the-coordinate-system-to-set-size-of-a-node?rq=1] is elucidating. – cxandru Dec 02 '19 at 11:40