TikZ - An arc with an arrow - latex

I am learning Tikz and hope somebody can help me achieve the following i.e. I want to draw a directed arc between objects in an equation. Below is a picture of what I am trying to achieve.
I have also attached the code I have used so far:
\documentclass{article}
\usepackage{amsmath,amssymb,braket,tikz}
\usetikzlibrary{tikzmark,calc}
\begin{document}
\begin{tikzpicture}
$(x+2)(x+3)$
\end{tikzpicture}
\end{document}
I also suspect that there is a way to specify a line or arc, between elements e.g. numbers and letters, without explicitly stating the coordinates. Is this the case? If it were, it would simplify the things I'm trying to achieve.
Any help would be very much appreciated.

The following solution draws an arc above the formula using arc; the actual angles and lengths may have to be adjusted. To get coordinates relative to the formula, the formula is wrapped into the node formula.
\documentclass{article}
\usepackage{amsmath,amssymb,braket,tikz}
\usetikzlibrary{tikzmark,calc}
\begin{document}
\begin{tikzpicture}
\node (formula) [] {$(x+2)(x+3)$};
\draw[-latex,red] ($(formula.north west)+(.4,0)$) arc
[
start angle=160,
end angle=20,
x radius=0.5cm,
y radius =0.5cm
] ;
\end{tikzpicture}
\end{document}
Output:

Another possibility with the tikzmark library:
\documentclass{article}
\usepackage{amsmath,amssymb,braket,tikz}
\usetikzlibrary{tikzmark,calc}
\begin{document}
\begin{equation}
(\tikzmarknode{a}{x}+2)(\tikzmarknode{b}{x}+3)
\end{equation}
\tikz[remember picture, overlay]{\draw[-latex,red] ([yshift=0.1em]a.north) to[bend left] ([yshift=0.1em]b.north);}
\end{document}

Related

Generating random points then connecting points less than certain distance away

I currently have the code below which produces the later output. I would like to generate the 10 points randomly as I already am. But instead of having blue dashed lines connecting to the the location (0,0), I want them to connect to the other dots if the distance is less than 4cm away.
I attempted things such as storing the data into arrays but updating and accessing the values was not working. I attempted nested for loops but handling the seed became difficult. What is a good way to do this?
\documentclass{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\draw[step=0.5cm,color=gray] (-2.5,-2.5) grid (2.5,2.5);
\pgfmathsetseed{2}
\foreach \x in {1,...,10}
{
\pgfmathrandominteger{\a}{-240}{240}
\pgfmathrandominteger{\b}{-240}{240}
\fill [color=red,anchor=center](\a*0.01,\b*0.01) circle (0.1);
% CHANGE HERE
\draw [color=blue,densely dotted] (\a*0.01,\b*0.01) -- (0.0,0.0);
};
\end{tikzpicture}
\end{document}
I seemed to have figured out a solution (not ideal but works for this case).
The key was to use the \pgfmathparse for performing the if statement to get a 0 or 1 to use in \ifnum.
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{math}
\begin{document}
\begin{tikzpicture}
\draw[step=0.5cm,color=gray] (-2.5,-2.5) grid (2.5,2.5);
\pgfmathsetseed{2}
\foreach \x in {1,...,10}
{
\pgfmathrandominteger{\a}{-240}{240}
\pgfmathrandominteger{\b}{-240}{240}
\fill [color=red,anchor=center](\a*0.01,\b*0.01) circle (0.1);
\pgfmathsetseed{2}
\foreach \y in {0,...,\x}
{
\pgfmathrandominteger{\c}{-240}{240}
\pgfmathrandominteger{\d}{-240}{240}
\tikzmath{\i=(\a*0.01-\c*0.01)^2;}
\tikzmath{\j=(\b*0.01-\d*0.01)^2;}
\tikzmath{\k=\i+\j;}
\pgfmathparse{\k < 4.0 ? 1 : 0}
\ifnum\pgfmathresult=1
\draw [color=blue,densely dotted] (\a*0.01,\b*0.01) -- (\c*0.01,\d*0.01);
\fi
};
};
\end{tikzpicture}
\end{document}

circuitikz - move current labels closer to current arrow

how can I move the current label closer to the current arrow. I.e. I want to move the i_1 closer to the arrow.
MWE:
\documentclass{standalone}
\usepackage[european,cuteinductors,fetbodydiode,straightvoltages]{circuitikz}
\begin{document}
\begin{figure}[htb]
\centering
\begin{tikzpicture}[scale=0.65, arrowmos]
\coordinate (zero) at (0,0);
\draw (zero) to[R,-*,R=$R_1$,i>_=$i_1$] ++(2.75,2);
\end{tikzpicture}
\end{figure}
\end{document}
I tried to put a \vspace in front of the label but it didn't work.
I propose two solutions here: the first one is using a fake label and then setting the label manually (with a lot of flexibility), or using the provided styling for labels. Details in comments; you need a quite recent circuitikz for using this solution (>=1.4.2).
\documentclass{standalone}
\usepackage[european,cuteinductors,fetbodydiode,straightvoltages]{circuitikz}
\begin{document}
\centering
\begin{tikzpicture}[scale=0.65, arrowmos]
\coordinate (zero) at (0,0);
% First option
% use a blank label for the current, and name the component
\draw (zero) to[R,-*,R=$R_1$,i>_=~,name=myI] ++(2.75,2);
% manually place the label where you like
% myIcurrent is the normal position where the blank label is set
\node[below=1mm, anchor=center, red] at (myIcurrent) {$i_1$};
% you can also use bipole current style to change inner sep;
\draw (2,0) to[bipole current style={inner sep=0pt}, R,-*,R=$R_1$,i>_=$i_1$] ++(2.75,2);
\end{tikzpicture}
\end{document}
See circuitikz manual, https://texdoc.org/serve/circuitikz/0#subsection.5.6 .

LaTeX - Simple Text over Image

I'm replicating an word documment and need to place some text over an image in the header, but i can't find a simple method to do that.
It's just some text centered over a retangular image, but the best anwser I've found, so far, for doing that is reading an 880 page manual for PFG package.
Is there anyway to do that with some simple package, like minipage?
One possible approach using tikz:
\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\node at (0,0) {\includegraphics[width=6cm]{example-image-duck}};
\node at (current bounding box.center) {Quack};
\end{tikzpicture}
\end{document}

Prevent expanding tikz picture to wrap text around

I am new to Tikz and can not figure out why relarivly small tikzpictures expand thier width to the entire page ones.
Since I want my texts floating around such figures, it would be very usefull to know how to prevent them from expanding.
Here are code and pictures of my problem and as you can see it's not caused by to long captions, what was my first idea.
\section{demo}
\begin{figure}[h]
\tdplotsetmaincoords{60}{25}
\begin{tikzpicture}[tdplot_main_coords, scale=1]
\coordinate (o) at (0,0,0);
\coordinate (x) at (4,0,0);
\coordinate (y) at (0,0,4);
\coordinate (z) at (0,-4,0);
\node[above] at (x) {x};
\node[above] at (y) {y};
\node[above] at (z) {z};
\draw[red, -latex] (o) -- (x);
\draw[green, -latex] (o) -- (y);
\draw[blue, -latex] (o) -- (z);
\end{tikzpicture}
\caption{far to long caption for this kind of sensless figure created just for demonstrationg tikzpicture expand their width}
\end{figure}
\begin{figure}[h]
%[... same code as above ...]
\end{figure}
Do you guys have any suggestions to fit the tikzpicture bounds to the contents?
Thanks.
This is the default behavior. You can use the wrapfigure package to wrap text around a tikz figure:
\begin{wrapfigure}{r}{0.4\textwidth}
\tdplotsetmaincoords{60}{25}
\begin{tikzpicture}[tdplot_main_coords, scale=1]
...
\end{tikzpicture}
\caption{far to long caption for this kind of sensless figure created just for demonstrationg tikzpicture expand their width}
\end{wrapfigure}
In \begin{wrapfigure}{r}{0.4\textwidth}, the first argument is the position (r for right), the second argument is the size of the wrapfigure (here 40% of the page width fits the tikz figure).

How to draw nodes with "stubs" in Latex?

I'm trying to draw this in latex.
Anyone know how?
Use the calc-library and do the following:
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}
\node[draw=black, circle, fill=green] (A) at (0,0) {A};
\draw let \p1=(A) in (A) -- (\x1+40,\y1+10);
\end{tikzpicture}
\end{document}
Using the let-statement it allows you to specify a reference point (or even more) to use it's coordinates for further calculation.

Resources