TIkZ-pic(带子图片)无法缩放

TIkZ-pic(带子图片)无法缩放

我读过了从直角坐标转换为极坐标但没有找到适合我的解决方案。

我已经定义了一个pic名称compass,计划在不同的缩放版本中多次使用它(并以不同的角度旋转)。

我可以定义第二个pic名为 的元素yacht,它是可重新定位、可缩放和可旋转的。唯一的区别是,我使用极坐标来构建罗盘玫瑰。

以下是预期用途的示例文档:

\begin{tikzpicture}
  \node (a) {A} ;
  \path pic (b) at ([xshift=5cm] a) {yacht} ;
  \pic [below=2cm of a, scale=.4, rotate=220] (c) {yacht} ;
  \pic [scale=.2, rotate=120, right=2cm of c-yacht] (d) {yacht} ;
  \pic [below=8cm of d-yacht, scale=.2, rotate=60] (error) {compass} ;
  \pic [right=9cm of error-compass ] (error) {compass} ;
\end{tikzpicture}

(为了进行比较,第一次调用yacht未缩放且未旋转。)

这就是结果。

无刻度罗盘玫瑰的示例

什么地方出了错?

这是完整的 MWE(我删除了的定义以yacht使其保持较小)。

\documentclass[12pt]{article}
\usepackage[ngerman]{babel}
\usepackage[T1]{fontenc}
\usepackage{tikz}

%% Lade weitere Pakete
\usetikzlibrary{
  positioning}



\def\needle{50}
\def\short{\needle/10}
\newcommand{\needlelength}[1]{\def\needle{#1}}

\tikzset{
  rose/.style = {
    % xscale      = 1/2,
    % yscale      = 1/2,
    % scale       = 0.2,
    font        =\large,
    semithick,
  },
  leftrose/.style = {
    rose,
    fill = black!70,
  },
  rightrose/.style = {
    fill = black!10,
  },
}

\tikzset{
  pics/compassneedle/.style 2 args={
    code={
      %% Try to draw a needle by aims of polar coordinates.
      \filldraw[leftrose]
      % start at origin, 
      (0:0)
      % move nw for the short distance
      -- ({#1+45}:{\short*\needle})
      % reach the tip at north
      -- ({#1}:{\needle})
      % go back to origin
      -- cycle ;
      % repeat the same on the eastern side.
      \filldraw[rightrose]
      % start at origin, 
      (0:0)
      % move ne for the short distance
      -- ({#1-45}:{\short*\needle})
      % reach the tip at north
      -- ({#1}:{\needle})
      % go back to origin
      -- cycle ;
      %%
      %% Insert the direction
      \node[rotate={#1-90}] at ({#1}:{1.1*\needle}) {#2} ;
    }
  }, % end of compassneedle
}

\tikzset{%
  pics/compass/.style={
    code={
      %% Position a named node here
      \node (-compass) {};

      %% Begin with a layer of the rose pointing to ne, nw, sw, se.
      %% This layer shall be somewhat shorter than the main layer.
      \needlelength{3.5}
      \foreach \angle/\name in {45/NE, 135/NW, 225/SW, 315/SE} {
        \pic{compassneedle={\angle}{\name}} ;
      }
      %% Next, put a second layer with the main directions.  Don't
      %% forget to increase the needle lengths accordingly.
      \needlelength{4}
      \foreach  \angle/\name in {0/E, 90/N, 180/W, 270/S} {
        \pic{compassneedle={\angle}{\name}} ;
      }
    }
  },
}
\makeatletter

\begin{document}

\begin{tikzpicture}
  \pic [scale=.2] (error) {compass} ;
  \pic [right=9cm of error-compass ] (error) {compass} ;
\end{tikzpicture}


\end{document}% 

答案1

\pic{compass}这是一个可以缩放和设计样式的(高尔夫类型)示例。

\documentclass[tikz,border=7pt]{standalone}
\tikzset{
  compass/.pic = {
    \foreach[count=\i,evaluate={\m=div(\i-1,4);\a=90*\i-45*(\m+1)}] \d in {NE,NW,SW,SE,E,N,W,S}{
      \filldraw[pic actions,rotate=\a,scale=.7+.3*\m] (0,0) -- (45:1)--(0:3) node[transform shape,rotate=-90,above]{\d};
      \filldraw[pic actions,fill=white,rotate=\a,scale=.7+.3*\m] (0,0) -- (-45:1)--(0:3)--cycle;
    };
  }
}
\begin{document}
  \begin{tikzpicture}
    \pic{compass};
    \pic[scale=.7,fill=red,text=blue] at (7,0) {compass};
  \end{tikzpicture}
\end{document}

在此处输入图片描述

答案2

正如@Kpym 在他的评论中所建议的那样,可以使用该选项transform canvas={scale=...}

因此,

\begin{tikzpicture}
  \pic [scale=.2] (error) {compass} ;
  \pic [right=9cm of error-compass ] (full-size) {compass} ;
\end{tikzpicture}

你应该使用

\begin{tikzpicture}
  \pic [transform canvas={scale=.2}] (error) {compass} ;
  \pic [right=9cm of error-compass ] (full-size) {compass} ;
\end{tikzpicture}

这对我来说很有效,但它把放在了pic图片的中间。所以你还必须在 x 和 y 上添加一些校正动作!

答案3

您还可以使用\resizebox{width}{height}{what you want to resize}

\documentclass[12pt]{article}
\usepackage[ngerman]{babel}
\usepackage[T1]{fontenc}
\usepackage{tikz}

%% Lade weitere Pakete
\usetikzlibrary{
  positioning}



\def\needle{50}
\def\short{\needle/10}
\newcommand{\needlelength}[1]{\def\needle{#1}}

\tikzset{
  rose/.style = {
    % xscale      = 1/2,
    % yscale      = 1/2,
    % scale       = 0.2,
    font        =\large,
    semithick,
  },
  leftrose/.style = {
    rose,
    fill = black!70,
  },
  rightrose/.style = {
    fill = black!10,
  },
}

\tikzset{
  pics/compassneedle/.style 2 args={
    code={
      %% Try to draw a needle by aims of polar coordinates.
      \filldraw[leftrose]
      % start at origin, 
      (0:0)
      % move nw for the short distance
      -- ({#1+45}:{\short*\needle})
      % reach the tip at north
      -- ({#1}:{\needle})
      % go back to origin
      -- cycle ;
      % repeat the same on the eastern side.
      \filldraw[rightrose]
      % start at origin, 
      (0:0)
      % move ne for the short distance
      -- ({#1-45}:{\short*\needle})
      % reach the tip at north
      -- ({#1}:{\needle})
      % go back to origin
      -- cycle ;
      %%
      %% Insert the direction
      \node[rotate={#1-90}] at ({#1}:{1.1*\needle}) {#2} ;
    }
  }, % end of compassneedle
}

\tikzset{%
  pics/compass/.style={
    code={
      %% Position a named node here
      \node (-compass) {};

      %% Begin with a layer of the rose pointing to ne, nw, sw, se.
      %% This layer shall be somewhat shorter than the main layer.
      \needlelength{3.5}
      \foreach \angle/\name in {45/NE, 135/NW, 225/SW, 315/SE} {
        \pic{compassneedle={\angle}{\name}} ;
      }
      %% Next, put a second layer with the main directions.  Don't
      %% forget to increase the needle lengths accordingly.
      \needlelength{4}
      \foreach  \angle/\name in {0/E, 90/N, 180/W, 270/S} {
        \pic{compassneedle={\angle}{\name}} ;
      }
    }
  },
}
\makeatletter

\begin{document}
\begin{figure}
    \resizebox{10cm}{!}{%
        \begin{tikzpicture}
            \pic (error) {compass} ;
            \pic [right=9cm of error-compass ] (error) {compass} ;
        \end{tikzpicture}
    }
\end{figure}
\end{document}

这样,您就不会遇到与transform canvas={scale=...}周围环境有关的问题。

編輯:新方式

\documentclass[12pt]{article}
\usepackage[ngerman]{babel}
\usepackage[T1]{fontenc}
\usepackage{tikz}
\usepackage{subfig} %% NEW PACKAGE
%% Lade weitere Pakete
\usetikzlibrary{
  positioning}
\def\needle{50}
\def\short{\needle/10}
\newcommand{\needlelength}[1]{\def\needle{#1}}
\tikzset{
  rose/.style = {
    % xscale      = 1/2,
    % yscale      = 1/2,
    % scale       = 0.2,
    font        =\large,
    semithick,
  },
  leftrose/.style = {
    rose,
    fill = black!70,
  },
  rightrose/.style = {
    fill = black!10,
  },
}
\tikzset{
  pics/compassneedle/.style 2 args={
    code={
      %% Try to draw a needle by aims of polar coordinates.
      \filldraw[leftrose]
      % start at origin, 
      (0:0)
      % move nw for the short distance
      -- ({#1+45}:{\short*\needle})
      % reach the tip at north
      -- ({#1}:{\needle})
      % go back to origin
      -- cycle ;
      % repeat the same on the eastern side.
      \filldraw[rightrose]
      % start at origin, 
      (0:0)
      % move ne for the short distance
      -- ({#1-45}:{\short*\needle})
      % reach the tip at north
      -- ({#1}:{\needle})
      % go back to origin
      -- cycle ;
      %%
      %% Insert the direction
      \node[rotate={#1-90}] at ({#1}:{1.1*\needle}) {#2} ;
    }
  }, % end of compassneedle
}
\tikzset{%
  pics/compass/.style={
    code={
      %% Position a named node here
      \node (-compass) {};

      %% Begin with a layer of the rose pointing to ne, nw, sw, se.
      %% This layer shall be somewhat shorter than the main layer.
      \needlelength{3.5}
      \foreach \angle/\name in {45/NE, 135/NW, 225/SW, 315/SE} {
        \pic{compassneedle={\angle}{\name}} ;
      }
      %% Next, put a second layer with the main directions.  Don't
      %% forget to increase the needle lengths accordingly.
      \needlelength{4}
      \foreach  \angle/\name in {0/E, 90/N, 180/W, 270/S} {
        \pic{compassneedle={\angle}{\name}} ;
      }
    }
  },
}
\makeatletter
\begin{document}
\begin{figure}
    \centering
    \subfloat{
        \raisebox{10mm}{
            \resizebox{3cm}{3cm}{
                \begin{tikzpicture}
                    \pic (error) {compass} ;
                \end{tikzpicture}
            }
        }
    }
    \subfloat{
        \resizebox{5cm}{5cm}{
            \begin{tikzpicture}
                \pic [right= 9cm of error-compass,anchor=center ] (error) {compass} ;
            \end{tikzpicture}
        }
    }
\end{figure}
\end{document}

第二种方式

在此处输入图片描述

希望这就是您所寻找的。

相关内容