tikzpictureベクトルの長さを使用してノードの次元を指定します

tikzpictureベクトルの長さを使用してノードの次元を指定します

を持つノードを作成したいと思いますminimum width=αx。ここで、x は tikzpicture 環境で指定された x 座標ベクトルの長さです (例 ) \begin{tikzpicture}[x=0.1cm,y=2.5ex]。これは可能ですか?

答え1

で示唆されているようにこの答え次の例のように、変数を定義してノードの x スケールと y スケール、およびスタイルを設定できます。

\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}

これにより、すべての環境にx特定のスケールを適用し、このスケールに基づいて長方形ノードのスタイルを定義します。ytikzpictureminimum widthminimum height

スケールは および\unitx変数に保存され\unity、ドキュメント内の任意の場所で再定義してスケールを変更できます。

スケーリングをグローバルに適用したくない場合は、コマンドevery tikzpictureの引数を削除し\tikzset、次の例のようにスケーリングをローカルに適用します。

\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}

関連情報