## LaTeX forum ⇒ General ⇒ How can I reduce my code? Topic is solved

LaTeX specific issues not fitting into one of the other forums of this category.
Tran Van Toan
Posts: 23
Joined: Sat Oct 28, 2017 1:18 am

### How can I reduce my code?

I draw a cone with base is a regular polygon with ten edgs. This is my code:
\documentclass[border=2mm,tikz]{standalone}\usepackage{fouriernc}\usepackage{tikz-3dplot}\usetikzlibrary{calc,backgrounds}\usepackage{tkz-euclide,amsmath}\usetkzobj{all}\usepackage{pgfplots} \begin{document} %polar coordinates of visibility\pgfmathsetmacro\th{60}\pgfmathsetmacro\az{130} \tdplotsetmaincoords{\th}{\az} %parameters of the cone\pgfmathsetmacro\R{3}\pgfmathsetmacro\v{5}  \begin{tikzpicture} [scale=1, tdplot_main_coords, axis/.style={blue,thick}]\pathcoordinate (O) at (0,0,0)coordinate (S) at (0,0,\v);\coordinate (A) at ($(O) + (360/10:{\R} and {\R})$);\coordinate (B) at ($(O) + (2*360/10:{\R} and {\R})$);\coordinate (C) at ($(O) + (3*360/10:{\R} and {\R})$);\coordinate (D) at ($(O) + (4*360/10:{\R} and {\R})$);\coordinate (E) at ($(O) + (5*360/10:{\R} and {\R})$);\coordinate (F) at ($(O) + (6*360/10:{\R} and {\R})$);\coordinate (G) at ($(O) + (7*360/10:{\R} and {\R})$);\coordinate (H) at ($(O) + (8*360/10:{\R} and {\R})$);\coordinate (I) at ($(O) + (9*360/10:{\R} and {\R})$);\coordinate (J) at ($(O) + (10*360/10:{\R} and {\R})$);\tkzDrawPoints[fill = black,size = 5 pt](S,A,B,C,D,E,F,O,G,H,I,J)% % computation of tangential points\pgfmathsetmacro\cott{{cot(\th)}}\pgfmathsetmacro\fraction{\R*\cott/\v}\pgfmathsetmacro\angle{{acos(\fraction)}} % % angles for transformed lines\pgfmathsetmacro\PhiOne{180+(\az-90)+\angle}\pgfmathsetmacro\PhiTwo{180+(\az-90)-\angle} % % coordinates for transformed surface lines\pgfmathsetmacro\sinPhiOne{{sin(\PhiOne)}}\pgfmathsetmacro\cosPhiOne{{cos(\PhiOne)}}\pgfmathsetmacro\sinPhiTwo{{sin(\PhiTwo)}}\pgfmathsetmacro\cosPhiTwo{{cos(\PhiTwo)}} % % angles for original surface lines\pgfmathsetmacro\sinazp{{sin(\az-90)}}\pgfmathsetmacro\cosazp{{cos(\az-90)}}\pgfmathsetmacro\sinazm{{sin(90-\az)}}\pgfmathsetmacro\cosazm{{cos(90-\az)}} % % draw basis circle\tdplotdrawarc[tdplot_main_coords,thick]{(O)}{\R}{\PhiOne}{360+\PhiTwo}{anchor=north}{}\tdplotdrawarc[tdplot_main_coords,dashed]{(O)}{\R}{\PhiTwo}{\PhiOne}{anchor=north}{} % % displaying tranformed surface of the cone (rotated)\draw[thick] (0,0,\v) -- (\R*\cosPhiOne,\R*\sinPhiOne,0);\draw[thick] (0,0,\v) -- (\R*\cosPhiTwo,\R*\sinPhiTwo,0); % % displaying original surface of the cone (rotated)\draw[thick] (S) -- (A) (S) -- (B)  (S) -- (C) (S)--(D) (S) -- (I) (S) -- (J)  (S) -- (H);\tkzDrawPolygon[dashed](A,B,C,D,E,F,G,H,I,J)%\draw[dashed] (A) -- (B) --(C)--(D)--(E)--(F) --(G)--(H)-- (I)-- (J) --(A) --cycle;\draw[dashed](S) --(E) (S) -- (F) (S) --(O) (S)--(G) (A)--(F) (B)--(G) (C)--(H) (D)--(I) (E)--(J);%\tkzLabelPoints[right](S,A,B,C,D,E,F,G,H,I,J)\tkzLabelPoints[above](S)\tkzLabelPoints[below](O)%\tkzFillPolygon[pattern=north west lines](A,B,C,D,E,F)\end{tikzpicture}\end{document}

How can I reduce the lines?
[code]\coordinate (A) at ($(O) + (360/10:{\R} and {\R})$);\coordinate (B) at ($(O) + (2*360/10:{\R} and {\R})$);\coordinate (C) at ($(O) + (3*360/10:{\R} and {\R})$);\coordinate (D) at ($(O) + (4*360/10:{\R} and {\R})$);\coordinate (E) at ($(O) + (5*360/10:{\R} and {\R})$);\coordinate (F) at ($(O) + (6*360/10:{\R} and {\R})$);\coordinate (G) at ($(O) + (7*360/10:{\R} and {\R})$);\coordinate (H) at ($(O) + (8*360/10:{\R} and {\R})$);\coordinate (I) at ($(O) + (9*360/10:{\R} and {\R})$);\coordinate (J) at ($(O) + (10*360/10:{\R} and {\R})$);

[/code]
Attachments
ScreenHunter 53.png (64.68 KiB) Viewed 2092 times

Tags:

rais
Posts: 256
Joined: Sun Nov 16, 2014 8:51 pm
You could use a \foreach statement instead, say
\foreach \a/\b in {A/1,B/2,C/3,D/4,E/5,F/6,G/7,H/8,I/9,J/10}    \coordinate (\a) at ($(O) + (\b*36:{\R})$);

KR
Rainer

Tran Van Toan
Posts: 23
Joined: Sat Oct 28, 2017 1:18 am
Thank you very much.

Stefan Kottwitz
Posts: 9557
Joined: Mon Mar 10, 2008 9:44 pm
Hi,

even simpler:

\foreach \i in {1,...,10}    \coordinate (\Alph{\i}) at ($(O) + (\i*36:{\R})$);

That of course requires a macro \Alph to turn numbers into letters. There is one internal macro, that could be used that way if you would add to the document preamble:

\makeatletter\let\Alph\@Alph\makeatother

Stefan

Tran Van Toan
Posts: 23
Joined: Sat Oct 28, 2017 1:18 am
Thank you very much. I still a small problem:
How can I reduce this command
\draw[dashed] (\Alph{1}) -- (\Alph{2})-- (\Alph{3}) -- (\Alph{4})--(\Alph{5}) -- (\Alph{6})-- (\Alph{7}) -- (\Alph{8})--(\Alph{9}) -- (\Alph{10})--cycle;

Stefan Kottwitz
Posts: 9557
Joined: Mon Mar 10, 2008 9:44 pm
For example:

\draw[red,foreach/.style={insert path=--(\Alph{#1})}] (A) [foreach/.list={2,...,10,1}] --cycle;

Stefan

Tran Van Toan
Posts: 23
Joined: Sat Oct 28, 2017 1:18 am
Thank you for all your helps. I repaired my code and got the same results.
A big problem is how to write a marco to draw style of line (dashed, not dashed) automatically. I can not solve this question by myselt.
This is solved by hand.

\foreach \i in {1,...,4,8,9,10} \draw[thick] (S)--(\Alph{\i});\foreach \i in {5,6,7} \draw[dashed] (S)--(\Alph{\i});

If regullar polygon with 20 edgs, it's difficult for me to choice style of lines.

And this is my full code.
\documentclass[border=2mm,tikz]{standalone}\usepackage{fouriernc}\usepackage{tikz-3dplot}\usetikzlibrary{calc,backgrounds}\usepackage{tkz-euclide,amsmath}\usetkzobj{all}\usepackage{pgfplots}\makeatletter\let\Alph\@Alph\makeatother\begin{document} %polar coordinates of visibility\pgfmathsetmacro\th{60}\pgfmathsetmacro\az{130}\tdplotsetmaincoords{\th}{\az}%parameters of the cone\pgfmathsetmacro\R{3}\pgfmathsetmacro\v{5}\begin{tikzpicture} [scale=1, tdplot_main_coords, axis/.style={blue,thick}]\pathcoordinate (O) at (0,0,0)coordinate (S) at (0,0,\v);\foreach \i in {1,...,10} \coordinate (\Alph{\i}) at ($(O) + (\i*36:{\R})$);%\foreach \i in {1,...,10} \tkzDrawPoints[fill = black,size = 5 pt] (\Alph{\i})\tkzDrawPoints[fill = black,size = 5 pt](S);% % computation of tangential points\pgfmathsetmacro\cott{{cot(\th)}}\pgfmathsetmacro\fraction{\R*\cott/\v}\pgfmathsetmacro\angle{{acos(\fraction)}} % % angles for transformed lines\pgfmathsetmacro\PhiOne{180+(\az-90)+\angle}\pgfmathsetmacro\PhiTwo{180+(\az-90)-\angle} % % coordinates for transformed surface lines\pgfmathsetmacro\sinPhiOne{{sin(\PhiOne)}}\pgfmathsetmacro\cosPhiOne{{cos(\PhiOne)}}\pgfmathsetmacro\sinPhiTwo{{sin(\PhiTwo)}}\pgfmathsetmacro\cosPhiTwo{{cos(\PhiTwo)}} % % angles for original surface lines\pgfmathsetmacro\sinazp{{sin(\az-90)}}\pgfmathsetmacro\cosazp{{cos(\az-90)}}\pgfmathsetmacro\sinazm{{sin(90-\az)}}\pgfmathsetmacro\cosazm{{cos(90-\az)}} % % draw basis circle\tdplotdrawarc[tdplot_main_coords,thick]{(O)}{\R}{\PhiOne}{360+\PhiTwo}{anchor=north}{}\tdplotdrawarc[tdplot_main_coords,dashed]{(O)}{\R}{\PhiTwo}{\PhiOne}{anchor=north}{} % % displaying tranformed surface of the cone (rotated)\draw[thick] (0,0,\v) -- (\R*\cosPhiOne,\R*\sinPhiOne,0);\draw[thick] (0,0,\v) -- (\R*\cosPhiTwo,\R*\sinPhiTwo,0); \draw[dashed,red,foreach/.style={insert path=--(\Alph{#1})}] (A) [foreach/.list={2,...,10,1}] --cycle; %\foreach \i in {1,...,10} \tkzLabelPoints[left](\Alph{\i});\foreach \i in {1,...,4,8,9,10} \draw[thick] (S)--(\Alph{\i});\foreach \i in {5,6,7} \draw[dashed] (S)--(\Alph{\i});\tkzLabelPoints[above](S)\tkzLabelPoints[below](O)\draw[dashed,blue] (\Alph{1})--(\Alph{6}) (\Alph{2})--(\Alph{7}) (\Alph{3})--(\Alph{8}) (\Alph{4})--(\Alph{9}) (\Alph{5})--(\Alph{10}); \draw[dashed,orange](S)--(O);\end{tikzpicture}\end{document}

Stefan Kottwitz
Posts: 9557
Joined: Mon Mar 10, 2008 9:44 pm
TikZ cannot automatically detect when a line is behind some area. The area is not even drawn. You have to decide what you make dashed for a simulated background appearance.

Anyway, just for better reading and for doing it in a simple loop, you could also write

\foreach \point/\style in {A/thick,  B/thick,  C/thick,  D/thick,                           E/dashed, F/dashed, G/dashed,                           H/thick,  I/thick,  J/thick}         \draw[\style] (S)--(\point);

Stefan