LaTeX forum ⇒ GeneralHow can I use \foreach to reduce this lines? 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 use \foreach to reduce this lines?

Postby Tran Van Toan » Thu May 03, 2018 10:37 am

I have a code
  1. \documentclass[border=2mm,12pt]{standalone}
  2. \usepackage{tikz}
  3. \usetikzlibrary{positioning,intersections,calc}
  4. \usepackage{fouriernc}
  5. \usepackage{tkz-euclide}
  6. \usetkzobj{all}
  7. \usetikzlibrary{shadings}
  8. \begin{document}
  9. \begin{tikzpicture}
  10. \def\h{4}
  11. \def\b{2}
  12. \def\c{5}
  13. \coordinate (A) at (0,0);
  14. \coordinate (B) at (\b,-1.5);
  15. \coordinate (C) at (\c,0);
  16. \coordinate (A_1) at (0,\h);
  17. \coordinate (B_1) at ($(B)+(0,\h)$);
  18. \coordinate (C_1) at ($(C)+(0,\h)$);
  19. \coordinate (M) at ($ (A_1)!0.5!(B_1) $ );
  20. \coordinate (N) at ($(B)!0.5!(C)$);
  21. \coordinate (K) at ($(B)!0.5!(B_1)$);
  22. \draw[dashed](A)--(C);
  23. \draw[thick](A_1)--(C_1);
  24. \tkzDrawPoints[fill = black,size = 5 pt](A,B,C,A_1,C_1,B_1)
  25. \tkzDrawPolygon[thick](A,B,B_1,A_1)
  26. \tkzDrawPolygon[thick](B,C,C_1,B_1)
  27. \tkzDrawPolygon[thick,pattern=vertical lines](C_1,K,N)
  28. \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}
  29. {\fill (\point) circle (2pt);
  30. \node[\position=2pt] at (\point) {$\point$};
  31. }
  32. \tkzMarkRightAngle(A,B,C)
  33. \tkzMarkRightAngle(A,B,C)
  34. \tkzMarkRightAngle(K,B,N)
  35. \tkzMarkRightAngle(B,C,C_1)
  36. \tkzMarkRightAngle(B,B_1,C_1)
  37. \tkzLabelSegment[below=2pt,midway](B,A){$4$}
  38. \tkzLabelSegment[below=2pt](N,C){$3$}
  39. \tkzLabelSegment[below=2pt](N,B){$3$}
  40. \tkzLabelSegment[left=2pt](B,K){$5$}
  41. \tkzLabelSegment[left=2pt](B_1,K){$5$}
  42. \tkzLabelSegment[right=2pt](C_1,C){$10$}
  43. \tkzLabelSegment[above=2pt](C_1,B_1){$6$}
  44. \tkzLabelSegment[below=2pt](M,B_1){$2$}
  45. \end{tikzpicture}
  46. \end{document}

How can I use \foreach to reduce this lines?
  1. \tkzLabelSegment[below=2pt](N,C){$3$}
  2. \tkzLabelSegment[below=2pt](N,B){$3$}
  3. \tkzLabelSegment[left=2pt](B,K){$5$}
  4. \tkzLabelSegment[left=2pt](B_1,K){$5$}
  5. \tkzLabelSegment[right=2pt](C_1,C){$10$}
  6. \tkzLabelSegment[above=2pt](C_1,B_1){$6$}
  7. \tkzLabelSegment[below=2pt](M,B_1){$2$}

Tags:

User avatar
Stefan Kottwitz
Site Admin
Posts: 9105
Joined: Mon Mar 10, 2008 9:44 pm

Postby Stefan Kottwitz » Thu May 03, 2018 11:34 am

For example this way:

  1. \foreach \direction/\p/\q/\name in {below/N/C/3,below/N/B/3,left/B/K/5,
  2. left/B_1/K/5, right/C_1/C/10,above/C_1/B_1/6,below/M/B_1/2} {
  3. \tkzLabelSegment[\direction=2pt](\p,\q){$\name$}
  4. }


The data is different from line to line, so we need several variables in the loop.

Stefan
Site admin

Tran Van Toan
Posts: 23
Joined: Sat Oct 28, 2017 1:18 am

Postby Tran Van Toan » Thu May 03, 2018 1:01 pm

Thank you very much.


Return to “General”

Who is online

Users browsing this forum: No registered users and 5 guests