2

I'd like to define a coordinate (Q1) which contains the x-component of a coordinate (P1) and as y-component the maximum y-values of (P1) and (P2).

Here's my MWE:

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}
\draw[gray!50, ultra thin] (0,0) grid (5,5);

\coordinate (P1) at (0,1);
\coordinate (P2) at (5,2);

\path let \p1=(P1),\p2=(P2) in coordinate (Q1) at (\x1, max(\y1,\y2));
\path let \p1=(P1),\p2=(P2) in coordinate (Q2) at (\x2, max(\y1,\y2));
%                                                       ^^^^^^^^^^^^^

\draw (P1) -- (P2);
\draw[dotted, red] (Q1) -- (Q2);

\end{tikzpicture}
\end{document}

I am looking for some kind of replacement for the underlined parts in my example above. I've found \pgfmathmax in the pgfmanual, but replacing the underlined parts with \pgfmathmax{\y1}{\y2} gives some error message.

4

You are only missing a pair of braces:

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}
\draw[gray!50, ultra thin] (0,0) grid (5,5);

\coordinate (P1) at (0,1);
\coordinate (P2) at (5,2);

\path let \p1=(P1),\p2=(P2) in coordinate (Q1) at (\x1, {max(\y1,\y2)});
\path let \p1=(P1),\p2=(P2) in coordinate (Q2) at (\x2, {max(\y1,\y2)});
%                                                       ^            ^

\draw (P1) -- (P2);
\draw[dotted, red] (Q1) -- (Q2);

\end{tikzpicture}
\end{document}

enter image description here

When TikZ looks for coordinates it uses delimited macros, so when you do (\x1, max(\y1,\y2)) it will look for something between ( and the next ), which is \x1, max(\y1,\y2, and things will go weird :). To avoid this you can "hide" the parentheses in braces: (\x1, {max(\y1,\y2)}).

| improve this answer | |
  • thank you for the explanation! now tikz makes a little more sense to me :) – thengineer Sep 1 '18 at 20:41
  • @thengineer You're welcome :) – Phelype Oleinik Sep 1 '18 at 20:42

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.