GeneralHow can I use \foreach to reduce this lines?

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

How can I use \foreach to reduce this lines?

Post by Tran Van Toan »

I have a code

Code: Select all

\documentclass[border=2mm,12pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning,intersections,calc}
\usepackage{fouriernc}
\usepackage{tkz-euclide}
\usetkzobj{all}
\usetikzlibrary{shadings}
\begin{document}
\begin{tikzpicture}
\def\h{4}
\def\b{2}
\def\c{5}
\coordinate (A) at (0,0);
\coordinate (B) at (\b,-1.5);
\coordinate (C) at (\c,0);
\coordinate (A_1) at (0,\h);
\coordinate (B_1) at ($(B)+(0,\h)$);
\coordinate (C_1) at ($(C)+(0,\h)$);
  \coordinate (M) at ($ (A_1)!0.5!(B_1) $ );
  \coordinate (N) at ($(B)!0.5!(C)$);
  \coordinate (K) at ($(B)!0.5!(B_1)$);
\draw[dashed](A)--(C);
\draw[thick](A_1)--(C_1);
\tkzDrawPoints[fill = black,size = 5 pt](A,B,C,A_1,C_1,B_1)
\tkzDrawPolygon[thick](A,B,B_1,A_1)
\tkzDrawPolygon[thick](B,C,C_1,B_1)
\tkzDrawPolygon[thick,pattern=vertical lines](C_1,K,N)
\foreach \point/\position in {A/left,B/below,C/right,A_1/above,B_1/above,C_1/above,M/above,N/below,K/left}
 {\fill (\point) circle (2pt);
   \node[\position=2pt] at (\point) {$\point$};
 }
\tkzMarkRightAngle(A,B,C)
\tkzMarkRightAngle(A,B,C)
\tkzMarkRightAngle(K,B,N)
\tkzMarkRightAngle(B,C,C_1)
\tkzMarkRightAngle(B,B_1,C_1)
\tkzLabelSegment[below=2pt,midway](B,A){$4$}
\tkzLabelSegment[below=2pt](N,C){$3$}
\tkzLabelSegment[below=2pt](N,B){$3$}
\tkzLabelSegment[left=2pt](B,K){$5$}
\tkzLabelSegment[left=2pt](B_1,K){$5$}
\tkzLabelSegment[right=2pt](C_1,C){$10$}
\tkzLabelSegment[above=2pt](C_1,B_1){$6$}
\tkzLabelSegment[below=2pt](M,B_1){$2$}
 \end{tikzpicture}
\end{document}
How can I use \foreach to reduce this lines?

Code: Select all

\tkzLabelSegment[below=2pt](N,C){$3$}
\tkzLabelSegment[below=2pt](N,B){$3$}
\tkzLabelSegment[left=2pt](B,K){$5$}
\tkzLabelSegment[left=2pt](B_1,K){$5$}
\tkzLabelSegment[right=2pt](C_1,C){$10$}
\tkzLabelSegment[above=2pt](C_1,B_1){$6$}
\tkzLabelSegment[below=2pt](M,B_1){$2$}

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org
LaTeX Beginner's Guide LaTeX Cookbook LaTeX TikZ graphics TikZによるLaTeXグラフィックス
User avatar
Stefan Kottwitz
Site Admin
Posts: 10290
Joined: Mon Mar 10, 2008 9:44 pm

How can I use \foreach to reduce this lines?

Post by Stefan Kottwitz »

For example this way:

Code: Select all

\foreach \direction/\p/\q/\name in {below/N/C/3,below/N/B/3,left/B/K/5,
  left/B_1/K/5, right/C_1/C/10,above/C_1/B_1/6,below/M/B_1/2} {
  \tkzLabelSegment[\direction=2pt](\p,\q){$\name$}
}
The data is different from line to line, so we need several variables in the loop.

Stefan
LaTeX.org admin
Tran Van Toan
Posts: 23
Joined: Sat Oct 28, 2017 1:18 am

How can I use \foreach to reduce this lines?

Post by Tran Van Toan »

Thank you very much.
Post Reply