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.

I found there are several ways to name a node (please relate to the MWE for the different numbers) but I am wondering if there are other (rather "simple") ways of doing this:

  1. My current 'best practice', it works with 2 node commands in a \draw command.
  2. My second way of doing this, it's very useful when paths based on names of nodes shall be produced, which again is usefull when an axis is scaled individually and therefor all other measures within a scope.
  3. The initial attempt which actually does not work as intended and lead to nr. 2.

Would anyone happen to have some advice to simplify the code? Or what else could be done? I'd appreciate anyh ints!

Display

MWE:

\documentclass[
a4paper
]{scrartcl}

\usepackage{
lmodern,
booktabs,
tikz
}
\usepackage[T1]{fontenc}

\begin{document}
\begin{center}
\begin{tikzpicture}[font=\sffamily\small]
    %
    \draw[style=help lines,step=0.5cm] (0,0) grid (6.2,6.2);
    %
    \draw[->,thick] (-0.1,0) -- (6.5,0) node[anchor=west]{x}; %X-Achse
    \draw[->,thick] (0,-0.1) -- (0,6.5) node[anchor=south]{y}; %Y-Achse
    %
    \foreach \x in {0,1,...,6} \draw [thick](\x cm,-2pt) -- (\x cm,2pt);
    \foreach \y in {0,1,...,6} \draw [thick](-2pt,\y) -- (2pt,\y);
    %
    \foreach \x in {0,1,...,6} \draw (\x cm, 0 cm) node[anchor=north]{\x};
    \foreach \y in {0,1,...,6}  \draw (0 cm, \y cm) node[anchor=east]{\y};
    %
    \begin{scope}[color=black]
    %1.
    \filldraw (2,1) circle (0.08cm) node (A) {} node[anchor=north,fill=white,yshift=-0.1cm]{A};
    %2.
    \filldraw (4,2) circle (0.08cm) node[anchor=west,fill=white] (B) {B};
    %3.
    \filldraw (3,5) circle (0.08cm) node[anchor=south,fill=white,yshift=0.1cm]{C};
    \node (C) at (3,5) {};
    \end{scope}
    \draw[very thick] (A.center) -- (B.center) -- (C.center) -- cycle;
\end{tikzpicture}
\end{center}
share|improve this question
1  
Your foreach loops are drawing independent paths so you can use one loop and many paths instead of one for each path. For example, just combine those four paths and use only \x. For the node placement you can use \node at (\x,0cm) {\x} instead of using a \draw each time. –  percusse Jun 24 '13 at 10:14

1 Answer 1

up vote 6 down vote accepted

A solution with a different approach: name your coordinates then add the labels.

enter image description here

The code:

\documentclass[tikz]{standalone}
\usepackage{lmodern}
\usepackage[T1]{fontenc}
\begin{document}
\begin{tikzpicture}[font=\sffamily\small]
    %
    \draw[style=help lines,step=0.5cm] (0,0) grid (6.2,6.2);
    %
    \draw[->,thick] (-0.1,0) -- (6.5,0) node[anchor=west]{x}; %X-Achse
    \draw[->,thick] (0,-0.1) -- (0,6.5) node[anchor=south]{y}; %Y-Achse
    %
    \foreach \x in {0,1,...,6} \draw [thick](\x cm,-2pt) -- (\x cm,2pt);
    \foreach \y in {0,1,...,6} \draw [thick](-2pt,\y) -- (2pt,\y);
    %
    \foreach \x in {0,1,...,6} \draw (\x cm, 0 cm) node[anchor=north]{\x};
    \foreach \y in {0,1,...,6}  \draw (0 cm, \y cm) node[anchor=east]{\y};
    % triangle with coordinates
    \draw[very thick]
    (2,1) coordinate (A)
    -- (4,2) coordinate (B)
    -- (3,5) coordinate (C)
    -- cycle;
    %
    \foreach \pt/\labpos in {A/below left,B/right,C/above}{
      \filldraw (\pt) circle(.8mm) node[\labpos=1.5mm,fill=white]{\pt};
    }
\end{tikzpicture}
\end{document}
share|improve this answer
    
Oh, very interesting. That's definitely a new one to me, plus it looks simpler. I think this also helped me a bit with defining \foreach-commands. –  henry Jun 24 '13 at 9:41

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.