6

How to create custom TikZ node "cylinder with bottom", where hidden line in cylinder bottom is shown using dashed line.

Something like the following creation, except node center should be in the middle of cylinder (as if it was in the middle of the center of 3D cylinder), not in center of the side -- it looks imbalanced

https://www.writelatex.com/1149537sksqzs#/2737083/

\documentclass[tikz]{standalone}

\usepackage{tikz}
\usetikzlibrary{shapes.geometric,calc}

\begin{document}
\begin{tikzpicture}
  \node[cylinder,draw=black,thick,aspect=0.7,
        minimum height=1.7cm,minimum width=1.5cm,
        shape border rotate=90,
        cylinder uses custom fill,
        cylinder body fill=red!30,
        cylinder end  fill=red!10]
   (A) {A};
  \draw[dashed]
    let \p1 = ($ (A.after bottom) - (A.before bottom) $),
        \n1 = {0.5*veclen(\x1,\y1)},
        \p2 = ($ (A.bottom) - (A.after bottom)!.5!(A.before bottom) $),
        \n2 = {veclen(\x2,\y2)}
  in
    (A.before bottom) arc [start angle=0, end angle=180,
    x radius=\n1, y radius=\n2];
\end{tikzpicture}
\end{document}

3D cylinder with bottom

  • 2
    It seams to be a problem of line width. Try x radius=\n1 - \pgflinewidth ? – Tarass Jun 3 '14 at 17:58
9

As Tarass correctly guessed, it is a problem of line width, but it affects both to the arc radius and to the initial point (A.before bottom). This is a possible fix:

\documentclass[tikz]{standalone}

\usepackage{tikz}
\usetikzlibrary{shapes.geometric,calc}

\title{TikZ: cylinder with bottom - example}
\begin{document}
\begin{tikzpicture}
  \node[cylinder,draw=black,thick,aspect=0.7,minimum height=1.7cm,minimum width=1.5cm,shape border rotate=90,cylinder uses custom fill, cylinder body fill=red!30,cylinder end fill=red!10] (A) {A};
  \draw[dashed]
    let \p1 = ($ (A.after bottom) - (A.before bottom) $),
        \n1 = {0.5*veclen(\x1,\y1)-\pgflinewidth},
        \p2 = ($ (A.bottom) - (A.after bottom)!.5!(A.before bottom) $),
        \n2 = {veclen(\x2,\y2)-\pgflinewidth}
  in
    ([xshift=-\pgflinewidth] A.before bottom) arc [start angle=0, end angle=180,
    x radius=\n1, y radius=\n2];
\end{tikzpicture}
\end{document}

enter image description here

  • Good shot, I haven't a tex box to try it. – Tarass Jun 3 '14 at 19:17
  • 1
    You should replace -\pgflinewidth with -0.5\pgflinewidth everywhere. – Nico Dec 22 '16 at 13:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.