5

I'd like to draw a set of lines between some set of points, and on the 1/3 and 2/3 point of each line I'd like to draw a vertical line. In order to do this I first defined the points, and then wanted to use a \foreach loop to draw. However it seems that the foreach loop does not work with calc:

The following code describes what I want to do. The problem is that the expression ($0.333*\x + 0.666*\y$) within the for loop does seem to cause problems. (While e.g. ($0.333*(A) + 0.666*(B)$) works.) Can anyone tell me what I'm doing wrong/what I need to change in order to make this work?

\documentclass{article} 
\usepackage{tikz}
\usepackage{pgfplots} 
\usetikzlibrary{patterns}
\usetikzlibrary{calc}
\usetikzlibrary{arrows.meta}
\usepackage{ifthen}
\begin{document}

\begin{center}
\begin{tikzpicture}
\coordinate (A) at (-3,1);
\coordinate (B) at (3,3);
\coordinate (C) at (0,3);
\foreach \x/\y in {(A)/(B),(B)/(C),(A)/(C)}{
\draw[-] \x -- \y; 
\draw[dashed] ($0.333*\x + 0.666*\y$) -- +(0,1);
\draw[dashed] ($0.666*\x + 0.333*\y$) -- +(0,1);
}
\end{tikzpicture}
\end{center}
\end{document}
8

The parentheses are needed as syntax characters inside the expression of TikZ library calc. But the coordinate names can be put into macros:

\coordinate (A) at (-3,1);
\coordinate (B) at (3,3);
\coordinate (C) at (0,3);
\foreach \x/\y in {A/B,B/C,A/C}{
  \draw[-] (\x) -- (\y);
  \draw[dashed] ($0.333*(\x) + 0.666*(\y)$) -- +(0,1);
  \draw[dashed] ($0.666*(\x) + 0.333*(\y)$) -- +(0,1);
}

Full example:

\documentclass{article}
\usepackage{tikz}
\usepackage{pgfplots}
\usetikzlibrary{patterns}
\usetikzlibrary{calc}
\begin{document}

\begin{center}
  \begin{tikzpicture}
    \coordinate (A) at (-3,1);
    \coordinate (B) at (3,3);
    \coordinate (C) at (0,3);
    \foreach \x/\y in {A/B,B/C,A/C}{
      \draw[-] (\x) -- (\y);
      \draw[dashed] ($0.333*(\x) + 0.666*(\y)$) -- +(0,1);
      \draw[dashed] ($0.666*(\x) + 0.333*(\y)$) -- +(0,1);
    }
  \end{tikzpicture}
\end{center}
\end{document}

Result

1
  • That is an even easier solution, thank you very much! – flawr Nov 10 '16 at 20:33
5

You have to expand \x and \y before you can feed them to the calc library. There are two ways, see example.

\documentclass{article}
\pagestyle{empty}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}

\begin{tikzpicture}
  \coordinate (A) at (-3,1);
  \coordinate (B) at (3,3);
  \coordinate (C) at (0,3);
  \foreach \x/\y in {(A)/(B),(B)/(C),(A)/(C)}{
    \draw[-] \x -- \y;

    % (1) expand only the point
    \edef\p{($0.333*\x + 0.666*\y$)}
    \draw[dashed] \p -- +(0,1);

    % (2) expand the whole path, protecting everything not to be
    % expanded by \noexpand
    \edef\a{\noexpand\draw[dashed] ($0.666*\x + 0.333*\y$) -- +(0,1);}\a
  }
\end{tikzpicture}

\end{document}

enter image description here

4
  • Thank you for your answer. What does to expand mean here? – flawr Nov 10 '16 at 20:12
  • @flawr It means that \x and \y are being replaced by their replacement text. You might be interested in these questions: tex.stackexchange.com/questions/451 tex.stackexchange.com/questions/248741 – Henri Menke Nov 10 '16 at 20:16
  • Oh now I see, thank you for the references. Just to be sure: Is it correct that in the second example you define \a to be the what is within the {}, where everything within the {} is evaluated except \draw[dashed]? (And afterwards we call \a to execute it?) – flawr Nov 10 '16 at 20:30
  • @flawr Everything inside the braces is expanded but most of it are letters which are unexpandable by definition. The only thing protected is the single token \draw. – Henri Menke Nov 10 '16 at 20:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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