0

I try to reproduce the right prism below in LaTeX :

enter image description here

I was able to create the shape but the tops seem to have problems? Also, how do I:

  1. add a color gradient
  2. color the base with another color and the edges in red
  3. add the corresponding letters

MWE :

\begin{tikzpicture}
\draw[thick] (1,0.5) -- (3,0) -- (5,0.5);
\draw[thick,dashed] (1,0.5) -- (1.5,1.5) -- (4.5,1.5) -- (5,0.5);
\draw[thick] (1,3.5) -- (3,3) -- (5,3.5) -- (4.5,4.5) -- (1.5,4.5) -- (1,3.5);
\draw[thick] (1,0.5) -- (1,3.5);
\draw[thick] (3,0) -- (3,3);
\draw[thick] (5,0.5) -- (5,3.5);
\draw[thick,dashed] (1.5,4.5) -- (1.5,1.5);
\draw[thick,dashed] (4.5,1.5) -- (4.5,4.5);
\end{tikzpicture}

enter image description here

2
  • Welcome to TeX.SX! I don't see any problem? What exactly to you mean? Dec 2, 2022 at 12:01
  • The edges are not correctly joined on my side on the PDF render. And I don't know how to color my shape, nor add the letters...
    – Sacha
    Dec 2, 2022 at 12:08

2 Answers 2

4

I would do this using 3d libraries. For example:

\documentclass[border=5mm,tikz]{standalone}
\usetikzlibrary{3d,calc,perspective}
 
\begin{document}
\begin{tikzpicture}[isometric view,rotate around z=180,line join=round,line cap=round]
% dimensions
\def\r{1}   % radius (circumcircle)
\def\h{1.5} % height
% coordinates
\foreach\i in {1,...,5}
{
  \coordinate (A\i) at (72*\i-27:\r);
  \coordinate (B\i) at ($(A\i)+(0,0,\h)$);
}
% color gradient
\fill[left color=white,right color=brown] (B5) -- (A5) -- (A1) -- (A2) -- (B2) -- (B1) -- cycle;
% visible lines
\draw (B5) -- (A5) -- (A1) -- (A2) -- (B2) node[midway,right,red] {$h$};
\draw (A1) -- (B1);
% not visible lines
\draw[dashed] (A2) -- (A3) -- (A4) -- (A5);
\draw[dashed] (A3) -- (B3);
\draw[dashed] (A4) -- (B4);
% yellow pentagon
\draw[red,fill=yellow,fill opacity=0.5] (B1) -- (B2) -- (B3) -- (B4) -- (B5) -- cycle;
\node[red] at (0,0,\h)  {$\mathcal{B}$};
\end{tikzpicture}
\end{document}

enter image description here

2
  • Thank you! Also, the render is not 2D but isometric view, how to fix that?
    – Sacha
    Dec 2, 2022 at 14:57
  • @Sacha, for an isometric view you put that option in the tikzpicture and you must load the perspective library. Dec 3, 2022 at 6:07
2

It might be a good idea to use polar coordinates, for example like this:

\documentclass[border=10pt]{standalone} 
\usepackage{tikz}

\begin{document}    

\begin{tikzpicture}
% fill/draw front part of cylinder
\fill[left color=orange!0, right color=orange!25] 
    (198:{1 and 0.5})
        -- ++(0,-1)
        -- ([shift={(down:1)}]270:{1 and 0.5})
        -- ([shift={(down:1)}]342:{1 and 0.5})
        -- ++(0,1)
        -- (270:{1 and 0.5})
        -- cycle;
\draw[thick, black] 
    (198:{1 and 0.5})
        -- ++(0,-1)
        -- ([shift={(down:1)}]270:{1 and 0.5})
        -- ([shift={(down:1)}]342:{1 and 0.5}) coordinate (a)
        -- ++(0,1) coordinate (b)
    (270:{1 and 0.5})
        -- ++(0,-1);

% draw invisible edges
\draw[thick, densely dashed]
    (54:{1 and 0.5})
        -- ++(0,-1)
        -- ([shift={(down:1)}]342:{1 and 0.5})
    (126:{1 and 0.5})
        -- ++(0,-1)
        -- ([shift={(down:1)}]198:{1 and 0.5})
    ([shift={(down:1)}]54:{1 and 0.5})
        -- ([shift={(down:1)}]126:{1 and 0.5});

% fill/draw top pentagon
\draw[thick, red, fill=yellow, fill opacity=0.5] 
    (270:{1 and 0.5}) coordinate (c)
        -- (342:{1 and 0.5})
        -- (54:{1 and 0.5})
        -- (126:{1 and 0.5})
        -- (198:{1 and 0.5})
        -- cycle;
\coordinate (d) at (90:{1 and 0.5});

% add annotations
\path (a) -- (b) node[midway, right, red] {$h$};
\path (c) -- (d) node[midway, red] {$\mathcal{B}$};

\end{tikzpicture}

\end{document}  

enter image description here

3
  • Thanks for the feedback!
    – Sacha
    Dec 2, 2022 at 14:57
  • @Sacha I think both solutions are valid. Both have their benefits. It depends on what you are doing with this. I tried to include a lot of syntax into my suggestion which may help you for future coding. Dec 2, 2022 at 15:06
  • Absolutely! I thank you for your annotations which will definitely help me.
    – Sacha
    Dec 2, 2022 at 15:09

You must log in to answer this question.

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