I try to draw beam on tikz with the possibility to hide its axis.
I created the following:
\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}
The result is fine, but my code not. I dislike 2 things: first, show axis parameter is not boolean and I have no ideas how to make it boolean, because I started to use tikz last Saturday. Second, it's not good to write show axis = yes every time I use pic {beam}. I found this answer, demonstrating, that it's possible to use boolean optional parameter for pic. Also I saw this post. I tried to adopt code from these posts but have a lot of errors. I can't understand why my code doesn't work because I use tikz only during for a week.
My code is:
\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}
