在 TikZ 中缩放装饰

在 TikZ 中缩放装饰

假设一幅绘图使用了一个简单的装饰(为了说明,假设是一个简单的弹簧):

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc,patterns,decorations.pathmorphing,decorations.markings}
\begin{document}
\begin{tikzpicture}[scale=2] %scale in question
\draw[decorate,decoration={zigzag,pre length=0.3cm,post length=0.3cm,segment length=6}] 
    (0,0) -- (2,0);
\end{tikzpicture}   
\end{document}

不包含以下内容的输出scale

无尺度

并且具有规模:

有尺度

是的,线条是缩放的,但装饰不是。是否有缩放装饰的选项?

春季装饰是一个最小的例子,更有趣的是更精美的装饰,例如tangent(来自这里):

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{decorations.markings}

\begin{document}

\begin{tikzpicture}[
tangent/.style={
    decoration={
        markings,% switch on markings
        mark=
        at position #1
        with
        {
            \coordinate (tangent point-\pgfkeysvalueof{/pgf/decoration/mark info/sequence number}) at (0pt,0pt);
            \coordinate (tangent unit vector-\pgfkeysvalueof{/pgf/decoration/mark info/sequence number}) at (1,0pt);
            \coordinate (tangent orthogonal unit vector-\pgfkeysvalueof{/pgf/decoration/mark info/sequence number}) at (0pt,1);
        }
    },
    postaction=decorate
},
use tangent/.style={
    shift=(tangent point-#1),
    x=(tangent unit vector-#1),
    y=(tangent orthogonal unit vector-#1)
},
use tangent/.default=1
]
\draw [tangent=0.7] (0,0) to [out=90,in=100] (2,0);
\draw [blue, thick, use tangent, ->] (0,0) -- (-1,0);
\end{tikzpicture}
\end{document}

创建以下输出:

无比例切线

添加scale=2

与比例尺相切

有没有什么办法可以避免这种情况?

答案1

简单的方法:

您可以定义 \myscale 参数并在图形中使用它:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc,patterns,decorations.pathmorphing,decorations.markings,shapes}

\begin{document}

\def\myscale{2}

\begin{tikzpicture}[scale=\myscale,
tangent/.style={
    decoration={
        markings,% switch on markings
        mark=
        at position #1
        with
        {
            \coordinate (tangent point-\pgfkeysvalueof{/pgf/decoration/mark info/sequence number}) at (0pt,0pt);
            \coordinate (tangent unit vector-\pgfkeysvalueof{/pgf/decoration/mark info/sequence number}) at (1,0pt);
            \coordinate (tangent orthogonal unit vector-\pgfkeysvalueof{/pgf/decoration/mark info/sequence number}) at (0pt,1);
        }
    },
    postaction=decorate
},
use tangent/.style={
    shift=(tangent point-#1),
    x=(tangent unit vector-#1),
    y=(tangent orthogonal unit vector-#1)
},
use tangent/.default=1
]
\draw [tangent=0.7] (0,0) to [out=90,in=100] (2,0);
\draw [blue, thick, use tangent, ->] (0,0) -- ({-1*\myscale},0);
\end{tikzpicture}

\begin{tikzpicture}[scale=\myscale] %scale in question
\draw[decorate,decoration={zigzag,
                                    pre length={0.3*\myscale cm},
                                    post length={0.3*\myscale cm},
                                    segment length={6*\myscale},
                                    amplitude={6*\myscale}}] 
    (0,0) -- (2,0);
\end{tikzpicture} 

\end{document}

输出:

在此处输入图片描述

重新缩放以检查结果。

相关内容