如何正确设置 tikz 中 pic 的可选布尔参数?

如何正确设置 tikz 中 pic 的可选布尔参数?

我尝试在 tikz 上绘制光束,并可以隐藏其轴。

我创建了以下内容:

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{math, shadings}

\usepackage{ifthen}

\tikzset{
    pics/beam/.style args={L #1 D #2 show axis #3}{
        code = {
            \tikzmath{
                \L = #1; % beam length
                \D = #2; % beam diameter
                \showAxis = "#3"; % yes if axis should be drawn, otherwise no
            }
            \begin{scope}[name prefix=beam pic]
                \coordinate (origin) at (0,0);
                \coordinate (beam end) at (\L, 0);
                \coordinate (p1) at (0, -\D/2);
                \coordinate (p2) at (\L, \D/2);
                
                \shadedraw [top color = gray, bottom color = gray, middle color = white] (p1) rectangle (p2);
                
                \ifthenelse{\equal{\showAxis}{yes}}{
                    \draw (origin) -- (-3.5, 0);
                    \draw [dash pattern = on 10 mm off 1 mm on 1 pt off 1 mm]  (0,0) -- (\L,0);
                    \draw (beam end) -- ++(3.5, 0);
                }{}
            \end{scope}
        }
    }
}

\begin{document}
    \begin{tikzpicture}[x=1mm, y=1mm]
        \draw (0,0) pic {beam = L 70 D 12 show axis yes};
    \end{tikzpicture}
\end{document}

在此处输入图片描述

结果很好,但我的代码不行。我不喜欢两件事:首先,参数不是布尔值,我不知道如何将其变为布尔值,因为我上周六才开始使用 tikz。其次,每次使用时都show axis写不好。我发现show axis = yespic {beam}这个答案,证明可以使用布尔可选参数pic。我还看到了这个帖子。我尝试采用这些帖子中的代码,但出现了很多错误。我不明白为什么我的代码不起作用,因为我只使用 tikz 一周。

我的代码是:

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{math}

\newif\ifShowAxis

\tikzset{
    pics/beam/.style = {
        code = {
            \tikzset{circle pyramid/.cd,#1}%
            \pgfkeysgetvalue{/tikz/circle pyramid options/L}\L
            \pgfkeysgetvalue{/tikz/circle pyramid options/D}\D
            \begin{scope}[name prefix=beam pic]
                \coordinate (origin) at (0,0);
                \coordinate (beam end) at (\L, 0);
                \coordinate (p1) at (0, -\D/2);
                \coordinate (p2) at (\L, \D/2);
                
                \shadedraw [top color = gray, bottom color = gray, middle color = white] (p1) rectangle (p2);
                
                \ifShowAxis
                    \draw (origin) -- (-3.5, 0);
                    \draw [dash pattern = on 10 mm off 1 mm on 1 pt off 1 mm]  (0,0) -- (\L,0);
                    \draw (beam end) -- ++(3.5, 0);
                \fi
            \end{scope}
        }
    },
    beam/.cd,
    L/.initial = 80,
    D/.initial = 12,
    show axis/.is if = ifShowAxis,
    show axis = true,
}

\begin{document}
    \begin{tikzpicture}[x=1mm, y=1mm]
        \draw (0,0) pic {beam};
    \end{tikzpicture}
\end{document}

答案1

过了一段时间我发现了回答,这对我帮助很大。

因此,我们可以像下面的代码一样使用 smth。

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{math}

\newif\ifshowaxis

\tikzset{
    beam parameters/.is family,
    beam parameters,
    L/.initial = 80, % beam length
    D/.initial = 12, % beam diameter
    show axis/.is if = showaxis, % not ifshowaxis!
    show axis = true,
}

\tikzset{
    pics/beam/.style = {
        code = {
            \tikzset{
                beam parameters/.cd,#1,
                L/.get = \L,
                D/.get = \D,
            }
            \begin{scope}[name prefix=beam pic]
                \coordinate (origin) at (0,0);
                \coordinate (beam end) at (\L, 0);
                \coordinate (p1) at (0, -\D/2);
                \coordinate (p2) at (\L, \D/2);
                
                \shadedraw [top color = gray, bottom color = gray, middle color = white] (p1) rectangle (p2);
                
                \ifshowaxis
                    \draw (origin) -- (-3.5, 0);
                    \draw [dash pattern = on 10 mm off 1 mm on 1 pt off 1 mm]  (0,0) -- (\L,0);
                    \draw (beam end) -- ++(3.5, 0);
                \fi
            \end{scope}
        }
    },
}

\begin{document}
    \begin{tikzpicture}[x=1mm, y=1mm]
        \draw (0,0) pic {beam};
        
        \draw (0, -20) pic {beam={show axis = false}};
    \end{tikzpicture}
\end{document}

简而言之,我们创建键系列,命名beam parameters,然后在 beam图片代码中使用这些键。

如果你检查这个答案,您可能会注意到,pic 代码之前没有键系列。我仍然无法理解并采用此答案中的代码来处理我的光束,但至少我以类似的方式解决了我的问题。

相关内容