TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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}
share|improve this question
up vote 8 down vote accepted

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

share|improve this answer
    
That is an even easier solution, thank you very much! – flawr 10 hours ago

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

share|improve this answer
    
Thank you for your answer. What does to expand mean here? – flawr 10 hours ago
    
@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 10 hours ago
    
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 10 hours ago
    
@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 10 hours ago

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.