TransWikia.com

Command to draw arc issue

TeX - LaTeX Asked by Octonions on April 23, 2021

I have the following code defined outside begin{document}:

newcommand{point}[2] {
    ({#1}, {#2})
}

newcommand{arcpoints}[6] {
    ({#1}:{atan((#5 - #3)/(#4 - #2))}:{#6})
}

I am getting an error when drawing the arc on the line above end{tikzpicture}:

begin{tikzpicture}[background rectangle/.style={fill=black}, show background rectangle]
    newcommand{f}[1] {
        {#1*#1 - #1/2 + 1/2}
    }

    draw[transparent] (-1, -1) grid (3, 3);
    draw[->] (-1, 0) -- (3, 0);
    draw[->] (0, -1) -- (0, 3);

    draw[dotted] point{1/4}{f{1/4}} -- point{1/4}{0};
    draw[dotted] point{3/2}{0} -- point{3/2}{f{3/2}};

    draw[dotted] point{0}{f{1/4}} -- point{1/4}{f{1/4}};
    draw[dotted] point{0}{f{3/2}} -- point{3/2}{f{3/2}};

    node[scale=0.6] at point{1/4}{-0.25} {$x$};
    node[scale=0.6] at point{3/2}{-0.25} {$x + h$};

    node[scale=0.6, left] at point{-0.1}{f{1/4}} {$fleft(xright)$};
    node[scale=0.6, left] at point{-0.1}{f{3/2}} {$fleft(x + hright)$};

    draw[blue, thin] point{1/4}{f{1/4}} -- point{3/2}{f{1/4}};
    draw[blue, thin] point{3/2}{f{1/4}} -- point{3/2}{f{3/2}};
    draw[scale=1, domain={-1}:{1/4*(1 + 41^(1/2))}, smooth, variable=x, thick] plot ({x}, {f{x}});
    draw[red, thin] point{3/2}{f{3/2}} -- point{1/4}{f{1/4}};

    fill[red, smooth] point{1/4}{f{1/4}} circle (1.25pt);
    fill[red, smooth] point{3/2}{f{3/2}} circle (1.25pt);

    draw[green, smooth] point{1/4 + 0.3}{f{1/4}} arc arcpoints{0}{1/4}{f{1/4}}{3/2}{f{3/2}}{0.3};
end{tikzpicture}

However, it seems to work when I write:

draw[green, smooth] point{1/4 + 0.3}{f{1/4}} arc arcpoints{0}{1}{1}{2}{2}{0.3};

How can I fix this problem and what is wrong with my syntax?

2 Answers

As I see, your problem is already solved by @rmano answer (+1), however, I wouldn't recommend your approach of drawing your image.

  • It make (according to my opinion) make image code longer and less clear. Compare the following two notation defining coordinates in your picture:
    point{0}{f(14)}
          (0,{f(14)})

both defines the same coordinate.

  • For drawing angle designation TikZ has defined library angles by which you can simple draw it. Compare both code:
draw[green, smooth] point{1/4 + 0.3}{(1/4)} arc arcpoints{0}{1/4}{f(1/4)}{3/2}{f(3/2)}{0.3};
pic [draw=green, semithick, radius=7mm] {angle = C--A--B};

where coordinates A, B and C can be defined at drawing dotted blue lines (see code and image below).

Considering aforementioned and (re)define your picture elements style, I suggest (for learn LaTeX and TikZ) to start with the following MWE (Minimal Working Example) which reproduce your image:

documentclass[border=3.142592]{standalone}
usepackage{tikz}
usetikzlibrary{angles,
                backgrounds
                }

begin{document}
    begin{tikzpicture}[
background rectangle/.style={fill=yellow!20},           % <---
show background rectangle,
tcklbl/.style = {inner sep=1pt, font=tiny},      % <---
declare function = {f(t)=((t)^2 - t/2 + 1/2);}       % <===
                        ]
% axis, grid
draw[gray=50, very thin] (-1, -1) grid (3, 3);
draw[->] (-1, 0) -- (3, 0);
draw[->] (0, -1) -- (0, 3);
% function
draw[thick] plot[samples=51, variable=x,
                  domain=-1:1.8507] (x, {f(x)});
% points on function
draw[densely dotted, blue] 
    (0,{f(1/4)}) node[tcklbl,left] {$f(x)$}     -| coordinate (A) (1/4,0) node[tcklbl,below] {$x$};
draw[densely dotted, blue] 
    (0,{f(3/2)}) node[tcklbl,left] {$f(x{+}h)$} -| coordinate (B) (3/2,0) node[tcklbl,below] {$x{+}h$};
draw[semithick, blue]      (B) |- coordinate (C) (A);
draw[semithick, red,fill]  (A) circle[radius=1pt] -- (B) circle[radius=1pt];
%
pic [draw=green, semithick, radius=7mm] {angle = C--A--B};
    end{tikzpicture}
end{document}

enter image description here

Addendum: In upper MWE the upper limits od function domain is calculated by calculator, but if you like to exercise, how to do this inside image code, than explore math library (TikZ & PGF manual, chapter 94 Mathematical Expressions (pp. 1029).

By use of pgfmathsetmacro{<macro name>}{<expression>} you can calculate as follows:

  • function: $f(x) = x^2 - frac{x}{2} + frac{1}{2}$ item $x$ where $f(x)=3$ can be calculated by:
    begin{align*}
x^2 - frac{x}{2} + frac{1}{2} & = 3 Rightarrow   
                   2x^2 - x - 5 & = 0 Rightarrow   
                              x & = frac{1 + sqrt{1 - 4cdot2cdot (-5)}}{2cdot2}    
                                & = frac{1 + sqrt{41}}{4} = boxed{1.8507}
   end{align*}

Considering above, the following changes in MWE should be done:

  • after tikzpicture options add
% right limit of domain:
pgfmathsetmacro{rbd}{(1 + sqrt(1 - 4*2*(-5)))/4}% axis, grid
  • code lines for drawing of the function replace width:
% function
draw[thick] plot[samples=51, variable=x,
                  domain=-1:rbd] (x, {f(x)});    % see rbd

Correct answer by Zarko on April 23, 2021

I am not sure about what you are trying to do here, but --- macro expansion in paths is not straightforward --- a macro is not a function, so you can have strange surprises (for example, #1^2 if passed -2 becomes -2^2 which will give -4...).

I would use a function, really:

documentclass[border=10pt]{standalone}
usepackage{tikz}
newcommand{point}[2] {
    ({#1}, {#2})
}

newcommand{arcpoints}[6] {
    ({#1}:{atan((#5 - #3)/(#4 - #2))}:{#6})
}
begin{document}
begin{tikzpicture}[declare function={
        f(x)=(x)*(x)-(x)/2+1/2;
    },]
draw[dotted] point{1/4}{f(1/4)} -- point{1/4}{0};
    draw[dotted] point{3/2}{0} -- point{3/2}{f(3/2)};
    draw[green, smooth] point{1/4 + 0.3}{(1/4)} arc arcpoints{0}{1/4}{f(1/4)}{3/2}{f(3/2)}{0.3};
end{tikzpicture}
end{document}

...although I still do not understand the point of point... (pun intended ;-))

Answered by Rmano on April 23, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP