在两行中附加相同的信息

在两行中附加相同的信息

这是可从以下网址获得的副本:蒂尔·坦陶

\documentclass{standalone}

\usepackage{tikz}
%\usetikzlibrary{trees,snakes}
\usepackage{verbatim}

\begin{document}
\pagestyle{empty}

\begin{comment}
:Title: A picture for Karl's students
:Slug: tutorial
:Tags: Manual

This example is from the tutorial: A picture for Karl's students.

| Author: Till Tantau
| Source: The PGF/TikZ manual


\end{comment}

\begin{tikzpicture}[scale=3,cap=round]
  % Local definitions
  \def\costhirty{0.8660256}

  % Colors
  \colorlet{anglecolor}{green!50!black}
  \colorlet{sincolor}{red}
  \colorlet{tancolor}{orange!80!black}
  \colorlet{coscolor}{blue}

  % Styles
  \tikzstyle{axes}=[]
  \tikzstyle{important line}=[very thick]
  \tikzstyle{information text}=[rounded corners,fill=red!10,inner sep=1ex]

  % The graphic
  \draw[style=help lines,step=0.5cm] (-1.4,-1.4) grid (1.4,1.4);

  \draw (0,0) circle (1cm);

  \begin{scope}[style=axes]
    \draw[->] (-1.5,0) -- (1.5,0) node[right] {$x$};
    \draw[->] (0,-1.5) -- (0,1.5) node[above] {$y$};

    \foreach \x/\xtext in {-1, -.5/-\frac{1}{2}, 1}
      \draw[xshift=\x cm] (0pt,1pt) -- (0pt,-1pt) node[below,fill=white]
            {$\xtext$};

    \foreach \y/\ytext in {-1, -.5/-\frac{1}{2}, .5/\frac{1}{2}, 1}
      \draw[yshift=\y cm] (1pt,0pt) -- (-1pt,0pt) node[left,fill=white]
            {$\ytext$};
  \end{scope}

  \filldraw[fill=green!20,draw=anglecolor] (0,0) -- (3mm,0pt) arc(0:30:3mm);
  \draw (15:2mm) node[anglecolor] {$\alpha$};

  \draw[style=important line,sincolor]
    (30:1cm) -- node[left=1pt,fill=white] {$\sin \alpha$} +(0,-.5);

  \draw[style=important line,coscolor]
    (0,0) -- node[below=2pt,fill=white] {$\cos \alpha$} (\costhirty,0);

  \draw[style=important line,tancolor] (1,0) --
    node [right=1pt,fill=white]
    {
      $\displaystyle \tan \alpha \color{black}=
      \frac{{\color{sincolor}\sin \alpha}}{\color{coscolor}\cos \alpha}$
    } (intersection of 0,0--30:1cm and 1,0--1,1) coordinate (t);

  \draw (0,0) -- (t);

  \draw[xshift=1.85cm] node [right,text width=6cm,style=information text]
    {
      The {\color{anglecolor} angle $\alpha$} is $30^\circ$ in the
      example ($\pi/6$ in radians). The {\color{sincolor}sine of
        $\alpha$}, which is the height of the red line, is
      \[
      {\color{sincolor} \sin \alpha} = 1/2.
      \]
      By the Theorem of Pythagoras we have ${\color{coscolor}\cos^2 \alpha} +
      {\color{sincolor}\sin^2\alpha} =1$. Thus the length of the blue
      line, which is the {\color{coscolor}cosine of $\alpha$}, must be
      \[
      {\color{coscolor}\cos\alpha} = \sqrt{1 - 1/4} = \textstyle
      \frac{1}{2} \sqrt 3.
      \]%
      This shows that {\color{tancolor}$\tan \alpha$}, which is the
      height of the orange line, is
      \[
      {\color{tancolor}\tan\alpha} = \frac{{\color{sincolor}\sin
          \alpha}}{\color{coscolor}\cos \alpha} = 1/\sqrt 3.
      \]%
    };
\end{tikzpicture}

\end{document}

在第 27 行中,有一个 30 度的余弦值。

在此处输入图片描述

在第 89 行,我们有相同的信息,但采用分数形式。

在此处输入图片描述

问题如下:

有什么方法可以将这两条信息附加起来,以便通过更改其中任何一个值就可以自动更改另一个值?

我的目的是确保这两个信息是兼容的。我需要确保这两行具有相同的信息。

该行不需要是分数。可以是数字,如第 27 行所示。但我想只更改一行,另一行则自行更改。

在此处输入图片描述

答案1

通过定义一些宏,您可以使绘图依赖于单个宏的值。因此,如果您\Angle在下面的代码中更改,则会相应地计算坐标,将线条绘制到它们应该去的地方,并打印正确的数字。不过,这样您只能得到十进制数。

\documentclass[border=5mm]{standalone}
\usepackage{xfp}
\usepackage{tikz}
\usetikzlibrary{intersections}
\usepackage{verbatim}

\begin{document}
\pagestyle{empty}

\begin{comment}
:Title: A picture for Karl's students
:Slug: tutorial
:Tags: Manual

This example is from the tutorial: A picture for Karl's students.

| Author: Till Tantau
| Source: The PGF/TikZ manual
\end{comment}

\begin{tikzpicture}[
   scale=3,
   cap=round,
   axes/.style={},
   important line/.style={very thick},
   information text/.style={rounded corners,fill=red!10,inner sep=1ex},
   /pgf/number format/precision=3
   ]

  % define angle
  \newcommand\Angle{30}
  % calculate sine, cosine and tangent
  \newcommand{\SINE}{\fpeval{sind(\Angle)}}
  \newcommand{\COS}{\fpeval{cosd(\Angle)}}
  \newcommand{\TAN}{\fpeval{\SINE/\COS}}
  % determine position for sin/cos/tan nodes
  \pgfmathsetmacro{\LeftRight}{ifthenelse(\COS<0,"left","right")}
  \pgfmathsetmacro{\RightLeft}{ifthenelse(\COS<0,"right","left")}
  \pgfmathsetmacro{\AboveBelow}{ifthenelse(\SINE<0,"above","below")}

  % Colors
  \colorlet{anglecolor}{green!50!black}
  \colorlet{sincolor}{red}
  \colorlet{tancolor}{orange!80!black}
  \colorlet{coscolor}{blue}

  % The graphic
  \draw[style=help lines,step=0.5cm] (-1.4,-1.4) grid (1.4,1.4);

  \draw (0,0) circle (1cm);

  \begin{scope}[style=axes]
    \draw[->] (-1.5,0) -- (1.5,0) node[right] {$x$};
    \draw[->] (0,-1.5) -- (0,1.5) node[above] {$y$};

    \foreach \x/\xtext in {-1, -.5/-\frac{1}{2}, 1}
      \draw[xshift=\x cm] (0pt,1pt) -- (0pt,-1pt) node[below,fill=white]
            {$\xtext$};

    \foreach \y/\ytext in {-1, -.5/-\frac{1}{2}, .5/\frac{1}{2}, 1}
      \draw[yshift=\y cm] (1pt,0pt) -- (-1pt,0pt) node[left,fill=white]
            {$\ytext$};
  \end{scope}

  % use \Angle instead of 30 in coordinates
  \filldraw[fill=green!20,draw=anglecolor] (0,0) -- (3mm,0pt) arc(0:\Angle:3mm);
  \draw (\Angle/2:2mm) node[anglecolor] {$\alpha$};

  \draw[style=important line,sincolor]
    (\Angle:1cm |- 0,0) -- node[\RightLeft,fill=white] {$\sin \alpha$} (\Angle:1cm);

  \draw[style=important line,coscolor]
    (0,0) -- node[\AboveBelow,fill=white] {$\cos \alpha$} (\COS,0);

  % sign returns the sign of the number (i.e. -1, 0 or 1)
  % note that when a coordinate contains (), as in sign(\COS),
  % you need to put braces around it like I've done here,
  % otherwise the parser will be confused by the )
  \draw[style=important line,tancolor] ({sign(\COS)},0) --
    node [\LeftRight,fill=white]
    {
      $\displaystyle \tan \alpha \color{black}=
      \frac{{\color{sincolor}\sin \alpha}}{\color{coscolor}\cos \alpha}$
    } ({sign(\COS)},{\TAN*sign(\COS)}) coordinate (t);

  \draw (0,0) -- (t);

  \draw[xshift=1.85cm] node [right,text width=6cm,style=information text]
    {
      The {\color{anglecolor} angle $\alpha$} is $\Angle^\circ$ in the
      example. The {\color{sincolor}sine of
        $\alpha$}, which is the height of the red line, is
      \[
      % use \pgfmathprintnumber to reduce the number of decimals
      {\color{sincolor} \sin \alpha} = \pgfmathprintnumber{\SINE}.
      \]
      By the Theorem of Pythagoras we have ${\color{coscolor}\cos^2 \alpha} +
      {\color{sincolor}\sin^2\alpha} =1$. Thus the length of the blue
      line, which is the {\color{coscolor}cosine of $\alpha$}, must be
      \[
      {\color{coscolor}\cos\alpha} = \sqrt{1 - \pgfmathprintnumber{\fpeval{(\SINE)^2}}} = \pgfmathprintnumber{\fpeval{abs(\COS)}}.
      \]%
      This shows that {\color{tancolor}$\tan \alpha$}, which is the
      height of the orange line, is
      \[
      {\color{tancolor}\tan\alpha} = \frac{{\color{sincolor}\sin
          \alpha}}{\color{coscolor}\cos \alpha} = \pgfmathprintnumber{\TAN}.
      \]%
    };
\end{tikzpicture}

\end{document}

\newcommand\Angle{30}

在此处输入图片描述

答案2

您可以定义一个双管齐下的宏,其扩展取决于节点中设置为 true 的条件。

我还使用了xfp\fpeval来避免自己进行计算。

\documentclass{standalone}

\usepackage{tikz}
%\usetikzlibrary{trees,snakes}
\usepackage{verbatim,xfp}

\newcommand{\definemathexpr}[3]{%
  % #1 is a macro name
  % #2 is the textual representation
  % #3 is the value
  \newcommand{#1}{\iftextual#2\else#3\fi}%
}
\newif\iftextual

\begin{document}
\pagestyle{empty}

\begin{comment}
:Title: A picture for Karl's students
:Slug: tutorial
:Tags: Manual

This example is from the tutorial: A picture for Karl's students.

| Author: Till Tantau
| Source: The PGF/TikZ manual


\end{comment}

\begin{tikzpicture}[scale=3,cap=round,every node/.code=\textualtrue]
  % Local definitions
  \definemathexpr\costhirty{\frac{1}{2}\sqrt{3}}{\fpeval{cosd(30)}}

  % Colors
  \colorlet{anglecolor}{green!50!black}
  \colorlet{sincolor}{red}
  \colorlet{tancolor}{orange!80!black}
  \colorlet{coscolor}{blue}

  % Styles
  \tikzstyle{axes}=[]
  \tikzstyle{important line}=[very thick]
  \tikzstyle{information text}=[rounded corners,fill=red!10,inner sep=1ex]

  % The graphic
  \draw[style=help lines,step=0.5cm] (-1.4,-1.4) grid (1.4,1.4);

  \draw (0,0) circle (1cm);

  \begin{scope}[style=axes]
    \draw[->] (-1.5,0) -- (1.5,0) node[right] {$x$};
    \draw[->] (0,-1.5) -- (0,1.5) node[above] {$y$};

    \foreach \x/\xtext in {-1, -.5/-\frac{1}{2}, 1}
      \draw[xshift=\x cm] (0pt,1pt) -- (0pt,-1pt) node[below,fill=white]
            {$\xtext$};

    \foreach \y/\ytext in {-1, -.5/-\frac{1}{2}, .5/\frac{1}{2}, 1}
      \draw[yshift=\y cm] (1pt,0pt) -- (-1pt,0pt) node[left,fill=white]
            {$\ytext$};
  \end{scope}

  \filldraw[fill=green!20,draw=anglecolor] (0,0) -- (3mm,0pt) arc(0:30:3mm);
  \draw (15:2mm) node[anglecolor] {$\alpha$};

  \draw[style=important line,sincolor]
    (30:1cm) -- node[left=1pt,fill=white] {$\sin \alpha$} +(0,-.5);

  \draw[style=important line,coscolor]
    (0,0) -- node[below=2pt,fill=white] {$\cos \alpha$} (\costhirty,0);

  \draw[style=important line,tancolor] (1,0) --
    node [right=1pt,fill=white]
    {
      $\displaystyle \tan \alpha \color{black}=
      \frac{{\color{sincolor}\sin \alpha}}{\color{coscolor}\cos \alpha}$
    } (intersection of 0,0--30:1cm and 1,0--1,1) coordinate (t);

  \draw (0,0) -- (t);

  \draw[xshift=1.85cm] node [right,text width=6cm,style=information text]
    {
      The {\color{anglecolor} angle $\alpha$} is $30^\circ$ in the
      example ($\pi/6$ in radians). The {\color{sincolor}sine of
        $\alpha$}, which is the height of the red line, is
      \[
      {\color{sincolor} \sin \alpha} = 1/2.
      \]
      By the Theorem of Pythagoras we have ${\color{coscolor}\cos^2 \alpha} +
      {\color{sincolor}\sin^2\alpha} =1$. Thus the length of the blue
      line, which is the {\color{coscolor}cosine of $\alpha$}, must be
      \[
      {\color{coscolor}\cos\alpha} = \sqrt{1 - 1/4} = \textstyle
      \costhirty.
      \]%
      This shows that {\color{tancolor}$\tan \alpha$}, which is the
      height of the orange line, is
      \[
      {\color{tancolor}\tan\alpha} = \frac{{\color{sincolor}\sin
          \alpha}}{\color{coscolor}\cos \alpha} = 1/\sqrt 3.
      \]%
    };
\end{tikzpicture}

\end{document}

相关内容