tikz:箭头不与图片的其余部分成比例

tikz:箭头不与图片的其余部分成比例

我正在尝试将缩放后的图表放入另一张图片中(是的,我需要使用嵌套的 tikz 图片,但我找不到更好的方法),问题是箭头的缩放比例不正确,因此缩放后的图表看起来很奇怪。有什么方法可以正确缩放图表吗?

我尝试过\scalebox{.6}{...},这仅适用于 tikzpicture,但在嵌套的 tikzpicture 中,使用的边界框就像图形没有被缩放一样,这使定位变得很难看。

另外,我可以先生成一个 pdf 并将其包含在内,但这并不实用,因为我想在 beamer 幻灯片中使用它,并且我需要\onslide<...>在里面包含一些命令,所以这意味着我需要为每张幻灯片创建一个 tex/pdf...

奖励:如果该方法还允许链接到带有记住选项的嵌套 tikzpicture,您将获得奖励。

梅威瑟:

\documentclass[a4paper]{article}

\usepackage{tikz}

\begin{document}
Not scaled:
\begin{tikzpicture}[
    qubit/.style={draw, circle, color=black, anchor=center,
      fill={rgb,255:red,143;green,146;blue,237},
      inner sep=0pt,minimum width=5mm}
    ]
  \node[qubit] (q1) at (0,0){};
  \draw[red,-stealth] (q1.center) -- (q1.north);
\end{tikzpicture}


Scaled:
\begin{tikzpicture}[scale=.5, every node/.style={scale=.5},
    qubit/.style={draw, circle, color=black, anchor=center,
      fill={rgb,255:red,143;green,146;blue,237},
      inner sep=0pt,minimum width=5mm}
    ]
  \node[qubit] (q1) at (0,0){};
  \draw[red,-stealth] (q1.center) -- (q1.north);
\end{tikzpicture}
\end{document}

在此处输入图片描述

-- 编辑 -- 这里是包含嵌套 tikzpicture 的 MWE:

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{calc,positioning}

\begin{document}
Not scaled:
\begin{tikzpicture}[
  qubit/.style={draw, circle, color=black, anchor=center,
    fill={rgb,255:red,143;green,146;blue,237},
    inner sep=0pt,minimum width=5mm}
  ]
  \node[qubit] (q1) at (0,0){};
  \node[qubit,right=of q1] (q2) {};
  \draw[-] (q1) -- (q2);
  \draw[red,-stealth] (q1.center) -- (q1.north);
\end{tikzpicture}

Scale + transform shape:\\
\begin{tikzpicture}
  \node[circle,draw,fill=green] (A) {A};
  \node[circle,draw,fill=green,right=of A] (B) {B};
  \node[circle,draw,fill=green,above=of A] (C) {C};
  \node[above=of B.center,anchor=south east] (graph) {
    \begin{tikzpicture}[scale=0.5, every node/.style={transform shape},
      qubit/.style={draw, circle, color=black, anchor=center,
        fill={rgb,255:red,143;green,146;blue,237},
        inner sep=0pt,minimum width=5mm}
      ]
      \node[qubit] (q1) at (0,0){};
      \node[qubit,right=of q1] (q2) {};
      \draw[-] (q1) -- (q2);
      \draw[red,-stealth] (q1.center) -- (q1.north);
    \end{tikzpicture}
  };
  \node[fill=green,inner sep=0pt,minimum size=3pt] at (graph.north east) {};
  \node[fill=red,inner sep=0pt,minimum size=3pt] at (graph.north west) {};
  \node[fill=yellow,inner sep=0pt,minimum size=3pt] at (graph.south east) {};
  \node[fill=orange,inner sep=0pt,minimum size=3pt] at (graph.south west) {};
\end{tikzpicture}

transform canvas, good arrow size, bad position:\\
\begin{tikzpicture}
  \node[circle,draw,fill=green] (A) {A};
  \node[circle,draw,fill=green,right=of A] (B) {B};
  \node[circle,draw,fill=green,above=of A] (C) {C};
  \node[above=of B.center,anchor=south east] (graph2) {
    \begin{tikzpicture}[transform canvas={scale=0.5},
      qubit/.style={draw, circle, color=black, anchor=center,
        fill={rgb,255:red,143;green,146;blue,237},
        inner sep=0pt,minimum width=5mm}
      ]
      \node[qubit] (q3) at (0,0){};
      \node[qubit,right=of q3] (q4) {};
      \draw[-] (q3) -- (q4);
      \draw[red,-stealth] (q3.center) -- (q3.north);
    \end{tikzpicture}
  };
  \node[fill=green,inner sep=0pt,minimum size=3pt] at (graph2.north east) {};
  \node[fill=red,inner sep=0pt,minimum size=3pt] at (graph2.north west) {};
  \node[fill=yellow,inner sep=0pt,minimum size=3pt] at (graph2.south east) {};
  \node[fill=orange,inner sep=0pt,minimum size=3pt] at (graph2.south west) {};
\end{tikzpicture}

\end{document}

在此处输入图片描述

-- 编辑 2 -- 我xsavebox按照评论中的建议尝试,并且效果很好,只是它不处理投影仪覆盖,这对我来说是一个很大的问题……

\documentclass{beamer}
\usepackage{xsavebox}
\usepackage{tikz}
\usetikzlibrary{calc,positioning}

\begin{document}

\begin{frame}
Not scaled:
\begin{tikzpicture}[
  qubit/.style={draw, circle, color=black, anchor=center,
    fill={rgb,255:red,143;green,146;blue,237},
    inner sep=0pt,minimum width=5mm}
  ]
  \node[qubit] (q1) at (0,0){};
  \node[qubit,right=of q1] (q2) {};
  \draw[-] (q1) -- (q2);
  \draw[red,-stealth] (q1.center) -- (q1.north);
\end{tikzpicture}
\end{frame}

\begin{frame}
Scale + transform shape, good position, bad arrow size:\\
\begin{tikzpicture}
  \node[circle,draw,fill=green] (A) {A};
  \node[circle,draw,fill=green,right=of A] (B) {B};
  \node[circle,draw,fill=green,above=of A] (C) {C};
  \node[above=of B.center,anchor=south east] (graph) {
    \begin{tikzpicture}[scale=0.5, every node/.style={transform shape},
      qubit/.style={draw, circle, color=black, anchor=center,
        fill={rgb,255:red,143;green,146;blue,237},
        inner sep=0pt,minimum width=5mm}
      ]
      \node[qubit] (q1) at (0,0){};
      \node[qubit,right=of q1] (q2) {};
      \draw[-] (q1) -- (q2);
      \draw[red,-stealth] (q1.center) -- (q1.north);
      \node<2>[qubit,above=of q2] (q2bis) {};
    \end{tikzpicture}
  };
  \node[fill=green,inner sep=0pt,minimum size=3pt] at (graph.north east) {};
  \node[fill=red,inner sep=0pt,minimum size=3pt] at (graph.north west) {};
  \node[fill=yellow,inner sep=0pt,minimum size=3pt] at (graph.south east) {};
  \node[fill=orange,inner sep=0pt,minimum size=3pt] at (graph.south west) {};
\end{tikzpicture}
\end{frame}

\begin{frame}
  transform canvas, good arrow size, bad position:\\
  \begin{tikzpicture}
    \node[circle,draw,fill=green] (A) {A};
    \node[circle,draw,fill=green,right=of A] (B) {B};
    \node[circle,draw,fill=green,above=of A] (C) {C};
    \node[above=of B.center,anchor=south east] (graph2) {
      \begin{tikzpicture}[transform canvas={scale=0.5},
        qubit/.style={draw, circle, color=black, anchor=center,
          fill={rgb,255:red,143;green,146;blue,237},
          inner sep=0pt,minimum width=5mm}
        ]
        \node[qubit] (q3) at (0,0){};
        \node[qubit,right=of q3] (q4) {};
        \draw[-] (q3) -- (q4);
        \draw[red,-stealth] (q3.center) -- (q3.north);
        \node<2>[qubit,above=of q4] (q5) {};
      \end{tikzpicture}
    };
    \node[fill=green,inner sep=0pt,minimum size=3pt] at (graph2.north east) {};
    \node[fill=red,inner sep=0pt,minimum size=3pt] at (graph2.north west) {};
    \node[fill=yellow,inner sep=0pt,minimum size=3pt] at (graph2.south east) {};
    \node[fill=orange,inner sep=0pt,minimum size=3pt] at (graph2.south west) {};
  \end{tikzpicture}
\end{frame}


\begin{xlrbox}{mybox}
  \begin{tikzpicture}[
    qubit/.style={draw, circle, color=black, anchor=center,
      fill={rgb,255:red,143;green,146;blue,237},
      inner sep=0pt,minimum width=5mm}
    ]
    \node[qubit] (q3) at (0,0){};
    \node[qubit,right=of q3] (q4) {};
    \draw[-] (q3) -- (q4);
    \draw[red,-stealth] (q3.center) -- (q3.north);
    \node<2>[qubit,above=of q4] (q5) {};
  \end{tikzpicture}
\end{xlrbox}

\begin{frame}
xlrbox, good resize, no beamer overlay:\\
\begin{tikzpicture}
  \node[circle,draw,fill=green] (A) {A};
  \node[circle,draw,fill=green,right=of A] (B) {B};
  \node[circle,draw,fill=green,above=of A] (C) {C};
  \node[above=of B.center,anchor=south east] (graph2) {\scalebox{0.5}{\themybox}};
  \node[fill=green,inner sep=0pt,minimum size=3pt] at (graph2.north east) {};
  \node[fill=red,inner sep=0pt,minimum size=3pt] at (graph2.north west) {};
  \node[fill=yellow,inner sep=0pt,minimum size=3pt] at (graph2.south east) {};
  \node[fill=orange,inner sep=0pt,minimum size=3pt] at (graph2.south west) {};
\end{tikzpicture}
\end{frame}

\end{document}

答案1

随着xsavebox包中,您可以将 tikzpicture 保存在xlrbox,并在使用时缩放它:

\documentclass[a4paper]{article}

\usepackage{tikz}
\usepackage{xsavebox}

\begin{xlrbox}{mybox}
\begin{tikzpicture}[
    qubit/.style={draw, circle, color=black, anchor=center,
      fill={rgb,255:red,143;green,146;blue,237},
      inner sep=0pt,minimum width=5mm}
    ]
  \node[qubit] (q1) at (0,0){};
  \draw[red,-stealth] (q1.center) -- (q1.north);
\end{tikzpicture}%
\end{xlrbox}

\begin{document}
Not scaled: \themybox

Scaled: \scalebox{0.5}{\themybox}
\end{document}

这也解决了 s 的嵌套问题tikzpicture

在此处输入图片描述

编辑(作者 marmot,他很乐意为所有错误承担责任;-):您永远不应该嵌套tikzpictures。您可以使用范围,您可以使用local bounding box技巧为其指定名称,然后这些范围的行为将非常类似于节点(就记住和放置相对于它们的东西而言)。显然,在范围内,您可以以通常的方式处理所有覆盖。也许最简单的方法是加载库arrows.meta并缩小箭头。(\pgflowlevelsynccm由于额外的翻译,这里不能以直接的方式工作,否则它会自动将箭头缩放到正确的大小,但无论如何,这个命令应该非常小心地使用。)

\documentclass{beamer}
\usepackage{tikz}
\usetikzlibrary{calc,positioning,arrows.meta}

\begin{document}


\begin{frame}
Scale + transform shape, good position, bad arrow size:\\
\begin{tikzpicture}
  \node[circle,draw,fill=green] (A) {A};
  \node[circle,draw,fill=green,right=of A] (B) {B};
  \node[circle,draw,fill=green,above=of A] (C) {C};
  \coordinate[above=of B.center] (X);

    \begin{scope}[shift={($(X)+(-1,0)$)},
    transform shape,scale=0.5, 
      qubit/.style={draw, circle, color=black, anchor=center,
        fill={rgb,255:red,143;green,146;blue,237},
        inner sep=0pt,minimum width=5mm},local bounding box=graph]
      \node[qubit] (q1) at (0,0){};
      \node[qubit,right=of q1] (q2) {};
      \draw[-] (q1) -- (q2);
      \node<2>[qubit,above=of q2] (q2bis) {};
      \draw[red,-{Stealth[scale=0.5]}] (q1.center) -- (q1.north);
    \end{scope}
    % comparison
    \draw[red,-stealth] ([xshift=2mm,yshift=-1cm]q1.center) -- ([xshift=2mm]q1.north);
  \node[fill=green,inner sep=0pt,minimum size=3pt] at (graph.north east) {};
  \node[fill=red,inner sep=0pt,minimum size=3pt] at (graph.north west) {};
  \node[fill=yellow,inner sep=0pt,minimum size=3pt] at (graph.south east) {};
  \node[fill=orange,inner sep=0pt,minimum size=3pt] at (graph.south west) {};
\end{tikzpicture}
\end{frame}

\end{document}

答案2

箭头按线宽缩放(或多或少),但线宽不缩放。主要是因为太细的线很难看清。

\documentclass[varwidth]{standalone}

\usepackage{tikz}

\begin{document}
Not scaled:
\begin{tikzpicture}[
    qubit/.style={draw, circle, color=black, anchor=center,
      fill={rgb,255:red,143;green,146;blue,237},
      inner sep=0pt,minimum width=5mm},
      line width=\pgflinewidth % default value
    ]
  \node[qubit] (q1) at (0,0){};
  \draw[red,-stealth] (q1.center) -- (q1.north);
\end{tikzpicture}


Scaled:
\begin{tikzpicture}[scale=.5, every node/.style={scale=.5},
    qubit/.style={draw, circle, color=black, anchor=center,
      fill={rgb,255:red,143;green,146;blue,237},
      inner sep=0pt,minimum width=5mm},
      line width=0.5\pgflinewidth
    ]
  \node[qubit] (q1) at (0,0){};
  \draw[red,-stealth] (q1.center) -- (q1.north);
\end{tikzpicture}
\end{document}

演示


默认值:

\pgfdeclarearrow{
  name = Stealth,
  defaults = {
    length  = +3pt 4.5 .8,
    width'  = +0pt .75,
    inset'  = +0pt 0.325,
    line width = +0pt 1 1,
  },

相关内容