1

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}

enter image description here

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}

1 Answer 1

0

After some time I found this answer, that helped me a lot.

So, we can use smth. like the following code:

\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}

Shortly, we create keys family, named beam parameters and then use these keys in beam pic code.

If you check this answer, you may notice, that there is no keys family before pic code. I still can't understand and adopt code in this answer for my beam, but at least I solved my problem in a similar way.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .