2

I am trying to learn to use the \newcommand feature to produce frustums of different sizes, possibly with dotted lines making the frustum to a cone.

This line of code does not work -- clearly a problem with the arithmetic using the parameters -- I have been testing with an alternative and it appears to be the division that is causing the problem.

%\draw[semithick] (-#3,#1) ellipse ( (#2)*(#3)/((#1)+(#2) ) and (#2)*(#3)/(3*( (#1)+(#2) ) ) );%

My code is below

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\usepackage{amssymb}
\usetikzlibrary{math}


\newcommand{\Frustom}[3]{

\begin{tikzpicture}

\draw[dashed,color=gray] (0,0) arc (2:178:#3 and #3/5);
\draw[semithick] (-2*#3,0) -- (-#3,#1+#2);
\draw[semithick] (0,0) -- (-#3,#1+#2);
\draw[semithick] (0,0) arc (0:-180:#3 and #3/5);
%\draw[semithick] (-#3,#1) ellipse ( (#2)*(#3)/((#1)+(#2) ) and (#2)*(#3)/(3*( (#1)+(#2) ) ) );% right ellipse

\draw[semithick] (-#3,#1) ellipse ( #2*#3/9 and 12/27 ) ;

\end{tikzpicture}
} %

\begin{document}
    \Frustom{6}{3}{4} %frustum height, cone height, base radius
\end{document}
Bernard
  • 271,350
ToyBoy
  • 117

1 Answers1

4

Welcome to TeX.SX! The following works:

\documentclass{article}
\usepackage{tikz}

\newcommand*{\Frustum}[3]{%
  \begin{tikzpicture}

  \draw[dashed,color=gray] (0,0) arc (2:178:#3 and #3/5);
  \draw[semithick] (-2*#3,0) -- (-#3,#1+#2);
  \draw[semithick] (0,0) -- (-#3,#1+#2);
  \draw[semithick] (0,0) arc (0:-180:#3 and #3/5);

  \pgfmathsetmacro{\radx}{(#2)*(#3) / ((#1)+(#2))}
  \pgfmathsetmacro{\rady}{(#2)*(#3)/(3*( (#1)+(#2) ) )}
  \draw[semithick] (-#3,#1) ellipse[x radius=\radx, y radius=\rady];

  \end{tikzpicture}%
}

\begin{document}
  \Frustum{6}{3}{4} % frustum height, cone height, base radius
\end{document}

Screenshot

In case you need to have commas inside either \radx or \rady (for instance, if you use a function such as atan2), you'll need to help the TikZ parser using braces, like so:

\draw[semithick] (-#3,#1) ellipse[x radius={\radx},
                                  y radius={\rady}];

Otherwise, it will act as if the commas delimit options of the ellipse command.

Note: beware of the spaces you introduced inside your \Frustum command, before \begin{tikzpicture} and after \end{tikzpicture}. Look at the percent signs I added in order to avoid this (be sure to read this if you don't know the story). In case you really want \Frustum to start a new paragraph, it is better style, in my opinion, to use \par inside its definition.

frougon
  • 24,283
  • 1
  • 32
  • 55