tikz ノードの寸法

tikz ノードの寸法

高さや幅などのノードの寸法を取得するにはどうすればよいですかtikz? これらのパラメータを使用して、オブジェクトの配置を正確に計算したいと考えています。たとえば、<a.height>次の例を使用します。

\documentclass{article}
\thispagestyle{empty}
\usepackage{graphicx}
\usepackage{tikz}
\begin{document}
\begin{figure}
  \begin{tikzpicture}
    \node(a) {
      \includegraphics[width=0.8\textwidth]{example-image-a}
    };
    \node(b) at (0, <a.height>*3/4){label};
  \end{tikzpicture}
\end{figure}
\end{document}

答え1

ノードの寸法を知らなくても望ましい結果が得られる2つの例。2つ目はcalctikzlibraryを使用する例です。

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}
  \begin{tikzpicture}
    \node(a) {
      \includegraphics[width=0.8\textwidth]{example-image-a}
    };
    \path (a.center)--(a.north) node[pos=.75, right] {label without calc};
    \node[left] at ($(a.center)!.75!(a.north)$) {label with calc};
  \end{tikzpicture}
\end{document}

ここに画像の説明を入力してください

アップデート:

label without calc前の例の実際の位置からラベルを 2cm 右、1cm 上に移動したいとします\path (a.center)--(a.north) node[pos=.75, right] {label without calc}。ライブラリを使用してpositioning、パス上の対応するポイントを基準にノードを移動できます。この場合、 と記述できますabove right=1cm and 2cmが、配置にはアンカーが使用されsouth west、 が必要で、その後にオプションwestを追加します。anchor=westabove right

\documentclass[border=2mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc, positioning}
\begin{document}
  \begin{tikzpicture}
    \node[inner sep=0pt] (a) {
      \includegraphics[width=0.8\textwidth]{example-image-a}
    };
    \path (a.center)--(a.north) node[draw, pos=.75, above right=1cm and 2cm, anchor=west, red] (b) {label without calc};
    \node[left] (c) at ($(a.center)!.75!(a.north)$) {label with calc};

    %Some auxiliary elements.
    \draw[red,<->] (a.center)--(c.east) node[midway, right] {75\%};
    \draw[red,<->] (c.east)--(a.north) node[midway,right] {25\%};
    \fill[red] (c.east) circle (1pt);
    \draw[red,<->] (c.east)-|(b.west)  node[pos=0.25,below] {2 cm} node[pos=0.75,left] {1 cm};
  \end{tikzpicture}
\end{document}

ここに画像の説明を入力してください

関連情報