Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

This question is similar to How to improve my PSTricks code to construct a triangle diagram?.

Consider the following example.

Code

% pdflatex -shell-escape test.tex

\documentclass{article}

\usepackage{
  auto-pst-pdf,
  pst-eucl,
  pstricks-add
}

\begin{document}

\def\laengde{6}
\def\startX{3}
\def\startY{0}
\pstFPdiv\konstA{\laengde}{2}
\pstFPsub\Bmin{\startX}{\konstA}
\pstFPadd\Bmaks{\startX}{\konstA}
\pstFPadd\Hmin{\startY}{0}
\pstFPdiv\konstB{3 sqrt}{2}
\pstFPmul\Hmaks{\konstB}{\laengde}
\begin{pspicture}(\Bmin,\Hmin)(\Bmaks,\Hmaks)
 \cnodeput(\startX,\startY){A}{$1$}
 \cnodeput(!\startX\space \laengde\space 6 div sub \startY\space \laengde\space 3 sqrt 2 mul div add){B}{$2$}
 \cnodeput(!\startX\space \laengde\space 6 div add \startY\space \laengde\space 3 sqrt 2 mul div add){C}{$3$}
 \cnodeput(!\startX\space \laengde\space 3 div sub \startY\space \laengde\space 3 sqrt       div add){D}{$4$}
 \cnodeput(!\startX\space                          \startY\space \laengde\space 3 sqrt       div add){E}{$5$}
 \cnodeput(!\startX\space \laengde\space 3 div add \startY\space \laengde\space 3 sqrt       div add){F}{$6$}
 \cnodeput(!\startX\space \laengde\space 2 div sub \startY\space \laengde\space 3 sqrt 2 div mul add){G}{$7$}
 \cnodeput(!\startX\space \laengde\space 6 div sub \startY\space \laengde\space 3 sqrt 2 div mul add){H}{$8$}
 \cnodeput(!\startX\space \laengde\space 6 div add \startY\space \laengde\space 3 sqrt 2 div mul add){I}{$9$}
 \cnodeput(!\startX\space \laengde\space 2 div add \startY\space \laengde\space 3 sqrt 2 div mul add){J}{\scriptsize{$10$}}
\psset{
  linewidth=1.5pt,
  linecolor=red
}
 \ncline{A}{B}
 \ncline{B}{D}
 \ncline{D}{G}
 \ncline{G}{H}
 \ncline{H}{I}
 \ncline{I}{J}
 \ncline{J}{F}
 \ncline{F}{C}
 \ncline{C}{A}
\psset{
  linewidth=0.5pt,
  linecolor=black,
  LabelSep=0.8,
  arrows=<->
}
 \pstMarkAngle{J}{A}{G}{\SI{60}{\degree}}
 \pstMarkAngle{A}{G}{J}{\SI{60}{\degree}}
 \pstMarkAngle{G}{J}{A}{\SI{60}{\degree}}
\end{pspicture}

\end{document}

Output

enter image description here

Question

I have the desired output but the code is rather messy; can anyone help me simplify if?

share|improve this question

2 Answers 2

up vote 8 down vote accepted

The latest pstricks.tex is needed for \Cnodeput; it can be downloaded from http://texnik.dante.de/tex/generic/pstricks/ since TeX Live 2012 is closed for updates.

\documentclass{article}
\usepackage{auto-pst-pdf,pst-eucl}

\def\CirclePut#1{\pscircle*[linecolor=white]{\Radius}\pscircle{\Radius}\rput(0,0){#1}}
\def\NPcline#1#2#3#4{%
  \ncline[linecolor=red,linewidth=1.5pt]{#1}{#2}% the visible one
  \pcline[linestyle=none](#1)(#2)% the invisible for correct length
  \ncput[npos=0.333]{\CirclePut{#3}}%
  \ncput[npos=0.667]{\CirclePut{#4}}}

\def\laengde{6 } \def\Radius{3mm }
\begin{document}

\begin{pspicture}(\laengde,\laengde)
\psset{radius=\Radius}
\Cnodeput(!\laengde 2 div 0){N1}{1}
\Cnodeput(!0 \laengde 3 sqrt mul 2 div){N7}{7}
\Cnodeput(!\laengde dup 3 sqrt mul 2 div){N10}{10}
\NPcline{N1}{N7}{2}{4}
\NPcline{N7}{N10}{8}{9}
\NPcline{N1}{N10}{3}{6}
\pnode(!\laengde dup 2 div exch 3 sqrt mul 6 div 2 mul){N1-10}
  \rput(N1-10){\CirclePut{5}}
 \psset{linewidth=0.5pt,LabelSep=0.8,arrows=<->}
 \pstMarkAngle{N10}{N1}{N7}{$60^\circ$}
 \pstMarkAngle{N1}{N7}{N10}{$60^\circ$}
 \pstMarkAngle{N7}{N10}{N1}{$60^\circ$}
\end{pspicture}

\end{document}

Another possibility is to set the y unit to 0.866 (i.e., approximately sqrt(3)/2), then we have in both directions the same step width and can have a variable size:

\documentclass{article}
\usepackage[pdf]{pstricks}% for running pdflatex --shell-escape
\usepackage{pst-eucl}
\newcounter{No}
\def\CirclePut#1#2{\pscircle*[linecolor=white]{#2}\pscircle{#2}\rput(0,0){#1}}
\def\Triangle#1#2{%
\psset{radius=#2,yunit=0.866}\setcounter{No}{0}
\begin{pspicture}(#1,#1)
\pspolygon[linecolor=red,linewidth=1.5pt](0,#1)(#1,#1)(!#1 2 div 0)
\multido{\iRow=0+2,\iStep=1+1}{\numexpr#1/2+1}{%
  \multido{\iCol=0+2}{\iStep}{%
    \stepcounter{No}%
    \pnode(!#1 2 div \iStep\space sub 1 add \iCol\space add \iRow){N\theNo}
    \rput(N\theNo){\CirclePut{\theNo}{#2}}%
}}
 \psset{linewidth=0.5pt,LabelSep=0.8,arrows=<->}
 \edef\UpperLeft{\the\numexpr\theNo-#1/2\relax}
 \pstMarkAngle{N\theNo}{N1}{N\UpperLeft}{$60^\circ$}
 \pstMarkAngle{N1}{N\UpperLeft}{N\theNo}{$60^\circ$}
 \pstMarkAngle{N\UpperLeft}{N\theNo}{N1}{$60^\circ$}
\end{pspicture}}

\begin{document}

\Triangle6{3mm} \hspace{1cm}
\Triangle8{3mm}

\end{document}

enter image description here

share|improve this answer
    
Hmm. I actually get an error when compiling the first examples: ! Use of \cput@iv doesn't match its definition. <argument> \cput@iv { !\laengde 2 div 0} l.16 \Cnodeput(!\laengde 2 div 0){N1}{1}. Btw. can I compile with pdflatex instead of xelatex? –  Svend Tveskæg Apr 19 '13 at 7:34
    
Arrrh. I can compile the first example with pdflatex but the output is wrong; it's just a line of PSTricks code and two circles (placed oddly). The second example works when compiling with xelatex but not with pdflatex. –  Svend Tveskæg Apr 19 '13 at 7:39

Single:

enter image description here

\documentclass[pstricks]{standalone}
\usepackage{pst-node,pst-eucl}
\usepackage{multido}
\usepackage[nomessages]{fp}
\FPset\Distance{1}% in cm
\FPset\Radius{.25}% in cm
\psset{MarkAngleRadius=.4,LabelSep=.6,arrows=<->,arcsep=2\pslinewidth,arrowinset=0,arrowscale=0.6,dimen=outer}

\def\Picture#1{%
    \FPeval\Right{round((#1-1)*Distance/2+Radius:3)}%
    \FPeval\Left{round(-Right:3)}%
    \FPeval\Top{round((#1-1)*Distance*root(2,3)/2+Radius:3)}%
    \FPeval\Bottom{round(-Radius:3)}%
    \begin{pspicture}[showgrid=false](\Left,\Bottom)(\Right,\Top)
    \pstVerb{/Distance \Distance\space def /Radius \Radius\space def}%
    \pnodes{V}
        (!0 \Bottom\space Radius add)
        (!\Left\space Radius add \Top\space Radius sub)
        (!\Right\space Radius sub \Top\space Radius sub)
    \pspolygon[linecolor=red,linewidth=2\pslinewidth](V0)(V1)(V2)
    \pstMarkAngle{V0}{V1}{V2}{\tiny$60^\circ$}
    \pstMarkAngle{V1}{V2}{V0}{\tiny$60^\circ$}
    \pstMarkAngle{V2}{V0}{V1}{\tiny$60^\circ$}
    \multido{\iy=0+1}{#1}{%
        \multido{\ix=0+1}{\numexpr\iy+1}{%
        \pnode(!\iy\space  Distance mul 2 div neg \ix\space  Distance mul add \iy\space  Distance mul 3 sqrt mul 2 div){P}
        \FPeval\temp{round(\ix+(\iy+1)*\iy/2+1:0)}%
        \pscircle[fillstyle=solid,fillcolor=yellow](P){\Radius}
        \rput(P){\temp}}}
\end{pspicture}}

\begin{document}
\Picture{10}
\end{document}

Multiple:

enter image description here

\documentclass{article}
\usepackage[a3paper,margin=1cm,landscape]{geometry}
\usepackage{multicol}
\pagestyle{empty}
\usepackage{pst-node,pst-eucl}
\usepackage{multido}
\usepackage[nomessages]{fp}
\FPset\Distance{1}% in cm
\FPset\Radius{.25}% in cm
\psset{MarkAngleRadius=.4,LabelSep=.6,arrows=<->,arcsep=\pslinewidth,arrowinset=0,arrowscale=0.6,dimen=middle}

\def\Picture#1{%
    \FPeval\Right{round((#1-1)*Distance/2+Radius:3)}%
    \FPeval\Left{round(-Right:3)}%
    \FPeval\Top{round((#1-1)*Distance*root(2,3)/2+Radius:3)}%
    \FPeval\Bottom{round(-Radius:3)}%
    \begin{pspicture}[showgrid=false](\Left,\Bottom)(\Right,\Top)
    \pstVerb{/Distance \Distance\space def /Radius \Radius\space def}%
    \pnodes{V}
        (!0 \Bottom\space Radius add)
        (!\Left\space Radius add \Top\space Radius sub)
        (!\Right\space Radius sub \Top\space Radius sub)
    \pspolygon[linecolor=red](V0)(V1)(V2)
    \pstMarkAngle{V0}{V1}{V2}{\tiny$60^\circ$}
    \pstMarkAngle{V1}{V2}{V0}{\tiny$60^\circ$}
    \pstMarkAngle{V2}{V0}{V1}{\tiny$60^\circ$}
    \multido{\iy=0+1}{#1}{%
        \multido{\ix=0+1}{\numexpr\iy+1}{%
        \pnode(!\iy\space  Distance mul 2 div neg \ix\space  Distance mul add \iy\space  Distance mul 3 sqrt mul 2 div){P}
        \FPeval\temp{round(\ix+(\iy+1)*\iy/2+1:0)}%
        \pscircle[fillstyle=solid,fillcolor=yellow](P){\Radius}
        \rput(P){\temp}}}
\end{pspicture}}

\begin{document}
\begin{multicols}{3}
\begin{enumerate}
\setcounter{enumi}{2}
\multido{\i=3+1}{11}{\item\raisebox{-.5\height}{\Picture{\i}}}
\end{enumerate}
\end{multicols}
\end{document}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.