4

This MWE does work which tells me the let operation is correctly defined.

    \documentclass{amsart}
    \usepackage{tikz}
    \usetikzlibrary{calc}
    \begin{document}
    \begin{tikzpicture}
    \path
    let \n1 ={2}
    in
    coordinate [label= $N$] (B) at (\n1,\n1); %to show that \n1 does work as a value
     \draw (0,0)  arc [start angle=0, end angle=180, radius=2];
    \end{tikzpicture}
    \end{document}
\end{document}

The one below differs from that only by using \n1 to specify the radius. But it returns an error message of

! Undefined control sequence. \pgfk@/tikz/x radius ->\n

\documentclass{amsart}
    \usepackage{tikz}
    \usetikzlibrary{calc}
    \begin{document}
    \begin{tikzpicture}
    \path
    let \n1 ={2}
    in
    coordinate [label= $N$] (B) at (\n1,\n1); %to show that \n1 does work as a value
     \draw (0,0)  arc [start angle=0, end angle=180, radius=\n1];
    \end{tikzpicture}
    \end{document}
\end{document}

As @Zarko says, the syntax of the first picture is not exactly what the manual recommends. But this syntax is working for me in a specific tikzpicture with a much more complicated let operation. Here it is. Notice that commands after the let operation succeed at use thing base on \p1 through \p4 (so I think savenumber might work for me, if I figure out how) but the arc command will not let me use a value \n1:

\documentclass{amsart}
\usepackage{tikz}
\usetikzlibrary{calc,intersections}
\begin{document}
    \[
    \begin{tikzpicture}
    \path % Given endpoints \p1 and \p2 and the placement of the divide       everything but the semicircle radius is calculated
    let
    \p1 = (-2.2,0),
    \p2 = (2.2,0),
    \p{divide} = ($ (\p1) !.7! (\p2) $),
    \p{center} = ($ (\p1) !.5! (\p2) $), % center of the semicircle
    \p3 = ($ (\x1,{\x{divide}-\x2}) $),
    \p4 =  ($ (\x{divide},\y3) $)
    in
    coordinate [label= left:$B$] (B) at (\p1)
    coordinate  [label= right:$F$] (F) at (\p2)
    coordinate  [label= left:$C$] (C) at (\p3)
    coordinate  [label= right:$D$] (D) at (\p4)
    coordinate  [label= below right:$E$] (E) at (\p{divide})
    coordinate (M) at (\p{center});  %the midpoint M is not labelled in the drawing

    \path [name path=R] (E) -- ($(E) + 1*(0,2.5)$);%upper bound here is set by hand

    \draw (F) [name path=P] arc [start angle=0, end angle=180, radius=2.2];%radius here is set by hand

    \draw [name intersections={of=P and R, by={[label=above:$H$]H}}];

    \draw[dashed]  (0,0) -- (H);
    \draw (B) -- (F) -- (H) --(B) -- (C) -- (D) -- (H);  %all the solid straight lines
    \end{tikzpicture}
    \]
    \end{document}

I need this tikzpicture for use in a publication. I could just use it the way I have currently written it and publish. But this version is inelegant because when I want to alter it a little to see what proportions look best I have to recalculate one radius by hand. If there is a way to get arc to accept \n1 as a radius value the same way as ``coordinate'' does does, with this syntax for let, then I will use that.

  • 1
    \n1 will not be available in the rest of the picture after the current path with let operation is finished. In other words, next line with \draw will not know about the previous \p{} and \n{} macros. – percusse Aug 28 '16 at 10:40
  • 1
    What is the more complicated tikzpicture you're trying to make? In any case, does the solution in this Jake's answer work? I think it's the best you can get in this situation. – Alenanno Aug 28 '16 at 10:58
  • @Alenanno Yes, savenumber looks like it might work for me. I will see. – Colin McLarty Aug 28 '16 at 11:24
  • Add \pgfmathsetmacro{\circleradius}{2.2} e.g. at the start of the tikzpicture, and use \circleradius instead of 2.2 everywhere. – Torbjørn T. Aug 28 '16 at 11:35
  • @TorbjørnT. Thanks, But that is the radius that I would like to be able to vary when I change \p1 and \p2, without just recalculating the radius by hand. – Colin McLarty Aug 28 '16 at 11:38
  • Well, you use \circleradius in \p1 and \p2 as well of course, so you have \p1=(-\circleradius,0),\p2=(\circleradius,0). – Torbjørn T. Aug 28 '16 at 11:40
  • Please, see your code again! You in it do exactly the same as I suggest in my answer ... All later used coordinates (B, C, ...) are defined in one \path let ... block. Or do I miss something? – Zarko Aug 28 '16 at 11:59
  • @Zarko Yes, that is what works in the code I have, but I cannot get it to work for the radius of the circle $F$, because I have to draw a line and calculate an intersection to find the radius. – Colin McLarty Aug 28 '16 at 13:06
  • you need define radious inside block let. \n1 is not valid outside it. by the way, this arc you can named, so as you named other lines. – Zarko Aug 28 '16 at 13:33
  • @TorbjørnT. Good point Maybe I should redesign around that. In fact just now percusse has done that in his answer, and that way eliminated my use of let altogether. – Colin McLarty Aug 28 '16 at 14:06

2 Answers2

6

You have wrong syntax. Try the following:

\documentclass[tikz,border=3mm]{standalone}
\usetikzlibrary{calc}

\begin{document}
\begin{tikzpicture}
\draw let \n1 = {2} in
        coordinate[label=above:$N$] (B) at (\n1,\n1)
        (0,0)  arc [start angle=0, end angle=180, radius=\n1];
\end{tikzpicture}
\end{document}

enter image description here

Edit: from TikZ manual:

Note that the effect of a let operation is local to the body of the let operation.

Addendum: I really have the problem to understand what cause your problems. Based on my guessing, i made the following MWE, which should show, that you can use my first MWE in your image on the following way (it is just dummy case, which should be simply extend to your real image):

\documentclass[tikz,border=3mm]{standalone}
\usetikzlibrary{calc,intersections}

\begin{document}
\begin{tikzpicture}
\draw[name path=A]
        let \n1 = {2} in
        coordinate[label=above:$N$] (B) at ( \n1,\n1)
        coordinate[label=below:$O$] (O) at (-\n1,0)
        (0,0)  arc [start angle=0, end angle=180, radius=\n1];
\draw[name path=C] (O) -- (B);
\fill[red,
      name intersections={of=A and C, by={D}}] (D) circle (2pt)
                                                   node[right=2mm] {intersection D};
\fill[gray] (O) circle (2pt)    (B) circle (2pt);
\end{tikzpicture}
\end{document}

enter image description here

Zarko
  • 296,517
  • 1
    Sorry, I do not understand your comment. I only show you, how to solve problem you indicate in your question. If this is not a problem, what it is? Please edit your question accordingly. Draw arc can be used in determining of intersection with some other line. – Zarko Aug 28 '16 at 07:51
  • I have edited the question. Since I am working on a specific complicated example which i am using for publication, there are many different issues I could ask about. This question is specifically about getting arc to accept a let operation in the same syntax that is working for coefficients and draw commands in my actual tikzpicture. – Colin McLarty Aug 28 '16 at 10:45
5

You can save whatever value you like into macros with various ways. You don't need to hack any let in machinary. But your picture is also pretty algorithmic so you don't need to save intermediate values that much, here is an alternative take

\documentclass[tikz]{standalone}
\usetikzlibrary{calc,intersections}
\begin{document}
\begin{tikzpicture}[]
\def\myradius{2.2}
\draw[name path=P] (0,0) coordinate(o)+(\myradius,0) coordinate[label=0:$F$] (f) 
    arc(0:180:\myradius) coordinate[label=180:$B$](b) --($(b)!0.7!(f)$) 
    coordinate[label=-45:$E$] (e) --cycle;
\draw(e)--($(e)!1!-90:(f)$) coordinate[label=0:$C$] -| (b) coordinate[label=180:$D$,pos=0.5];
\path [name path=R,overlay] (e) -- ++(0,1.5*\myradius);
\draw [dashed,name intersections={of=P and R, by=h}] (h)coordinate[label=60:$H$] -- (o);
\draw (b) -- (h) -- (f) (e) -- (h);
\end{tikzpicture}
\end{document}

Now two variables, \myradius and 0.7 defines the whole picture.

enter image description here

Torbjørn T.
  • 206,688
percusse
  • 157,807
  • Terrific. This teaches me about declaring coordinates inside a draw command, and calculating an intersection inside a draw command. I had not figured either of those out before. – Colin McLarty Aug 28 '16 at 13:18