tikz explain/annotate equation in LaTeX using straight arrows with right angles - latex

Thanks to this excellent answer, I can get an equation as in this figure: with bent arrows. But what I really want is as in this figure: with right-angled arrows.
To simply write the code, you may start by modifying the code from here.

Update: I've turned equation annotation into a LaTeX annotate-equations package (also on CTAN), which you might find useful.
Adapting Mike Renfro's answer
to straight edges is fairly straightforward: all I did was change from
\begin{tikzpicture}[overlay]
\path[->]<1-> (nCPP) edge [bend left] (tCPP);
\path[->]<2-> (nL) edge [bend left] (tL);
\path[->]<3-> (nPPP) edge [out=0, in=0] (tPPP);
\path[->]<4-> (nPP) edge [out=0, in=-90] (tPP);
\end{tikzpicture}
to
\begin{tikzpicture}[overlay,->]
\draw<1-> (nCPP) -| (tCPP);
\draw<2-> (nL) -| (tL);
\draw<3-> (nPPP) -| (tPPP);
\draw<4-> (nPP) -| (tPP);
\end{tikzpicture}
Changes explained:
replaced the edge and bend options with -| (there's also |- to bend at right angles the other way)
replaced \path with \draw to get the lines actually drawn
moved [->] into the tikzpicture environment options because for some reason it didn't work as \draw[->].
I've also set aspectratio=169 so there's enough space for the arrow for Predictor Prior Probability (it's also possible to work around that, but more hacky). This is what it looks like:

Related

Overlaying off-margin figure over another on Latex using Tikz

I was hoping anyone could help me with this. I am working with Latex and the Tikz package (I'm a new user on both) on a document cover using 2 figures so it looks like this:
That is, the background image leaves a thin margin on all sides, and the logo falls off-margin to the right and bottom. So, I enter the code overlaying the the logo over the background using Tikz, and as soon as I edit the coordinates to get the logo to go off-margin either on the right or bottom, the background image starts moving to the left and top until it sticks to the opposing borders of the page, like this:
My question is, is there a way to have the superimposing image to fall off the margins while keeping the background image properly centered?
Here is the code I'm using:
\usepackage{mwe,tikz}
\begin{figure}
\begin{tikzpicture}[
every node/.style={anchor=south west,inner sep=20pt},
x=1mm, y=1mm,
]
\node (fig1) at (0,0)
{\includegraphics[scale=0.2]{Images/Background.jpg}};
\node (fig2) at (124,-23)
{\includegraphics[scale=1.5]{Images/Logo.png}};
\end{tikzpicture}
\end{figure}
I first tried using the Overpic package instead, but I found it to be rather limited (or I couldn't figure it out well enough probably). I then tried using \centering and other horizontal and vertical centering techniques in combination with Tikz, but to no avail.
A figure environment is a floating objects, which allows latex to find a good location within the text flow. If you want an image at a very specific position, like a title page, you shouldn't use a figure environment
using the overlay option will make sure that the actual size of the tikzpicture does not influence the positioning and thus a cutoff logo won't influence the rest of the page
I also suggest the remember picture option which allows you to position your nodes with respect to the page. This way you can place the big picture in the centre of the page and the smaller picture at the lower right corner (shift it around with the xshift and yshift keys)
\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[remember picture,overlay]
\node at (current page.center) {\includegraphics[width=.9\paperwidth,height=.9\paperheight]{example-image-10x16}};
\node at ([xshift=-2cm,yshift=1cm]current page.south east) {\includegraphics[width=15cm]{example-image-duck}};
\end{tikzpicture}
\end{document}

Create new variable each time an environment in LaTeX is called

I've created an example environment for my maths notes. It takes the title of the example as the input and draws some lines with tikz. However, to do so, it requires the length of the title.
This is relatively easy to do when the environment is only called once by using \newlength{\lengthname} followed by \settowidth{\lengthname}{[length]}. However, as soon as it is called more than once, a different length must be defined. My (admittedly poor) work-around has been to pass the name of a different length, #2, every time I use my example environment.
How can I create a unique \newlength{\unique} each time I use my environment, or, is there some better way of achieving my goal?
\newenvironment{example}[2] % Example Environment
{\refstepcounter{example}
\newlength{#2}
\settowidth{#2}{\small \textbf{Example \thesection.\theexample} --- #1}
\bigskip\begin{tikzpicture}
\draw (-0.5\columnwidth,-0.2)--(-0.5\columnwidth,0)--(0.5\columnwidth,0)--(0.5\columnwidth,-0.2);
\fill[white] (-0.5#2-5pt,-1pt) rectangle (0.5#2+5pt,1pt);
\tikzlabel{0}{-0.4}{\text{\small \textbf{Example \thesection.\theexample} --- #1}}
\end{tikzpicture}}
%
{\begin{tikzpicture}
\draw (-0.5\columnwidth,0.2) -- (-0.5\columnwidth,0) -- (0.5\columnwidth,0) -- (0.5\columnwidth,0.2);
\end{tikzpicture}}
Many thanks.
My suggestion would be to use tcolorbox instead of drawing the frame yourself, but if you must use tikz, simply use a white background for your title.
Please note that your code would produce a lot of overfull box warnings. You have to consider the indention and drawing a frame of column won't fit because you need an additional two times the half the width of the tikz lines. I simply reduced the width to .49\columnwidth, but you could also take into account the width of the line in your calculation.
Also pay attention to the spacing around the ---. If you don't prevent the macro before from swallowing the space, it won't be centred.
\documentclass{article}
\usepackage{tikz}
\newcounter{example}
\newenvironment{example}[1]{%
\refstepcounter{example}%
\bigskip
\noindent%
\begin{tikzpicture}
\draw (-0.49\columnwidth,-0.2)--(-0.49\columnwidth,0)--(0.49\columnwidth,0)--(0.49\columnwidth,-0.2);
\node[fill=white,font=\small\bfseries] at (0,-1pt) {Example \thesection.\theexample{} --- #1};
\end{tikzpicture}%
\par%
}{%
\par%
\noindent%
\begin{tikzpicture}
\draw (-0.49\columnwidth,0.2) -- (-0.49\columnwidth,0) -- (0.49\columnwidth,0) -- (0.49\columnwidth,0.2);
\end{tikzpicture}%
\par%
}
\begin{document}
\begin{example}{test}
content...
\end{example}
\end{document}

How to create a mindmap in Latex with hexagonal shapes?

I'm busting my head on creating a mindmap in LaTex/TikZ with nodes being displayed not as boxes, circles or ellipses. Is there a way to create a mindmap using hexagons as children? The children ought to be connected to the corners. Parents and children should contain a short text
I've tried using the shapes package.
Thank you in advance,
Pjotr
Altering the shapes of the child nodes is easy, you could simply use regular polygon, regular polygon sides=6 from the shapes library. However altering the connections between the root node and the hexagones is more difficult (at least for us mere mortals, tikz wizards can do it, see https://tex.stackexchange.com/a/514772/36296).
Instead you could emulate a mindmap with normal nodes:
\documentclass[margin=0.3cm]{standalone}
\usepackage{tikz}
\usetikzlibrary{shapes}
\begin{document}
\begin{tikzpicture}
\def\nchilds{5}
\node[fill=blue,text=white,circle,minimum size=3cm] at (0,0) (root) {Root};
\foreach \x [count=\xi] in {test1, test2, test3, test4, test5}{
\shade[left color=red,right color=blue,shading angle={(360/\nchilds*\xi)-90}]
({(360/\nchilds*\xi)+3}:3.2cm) to [in=160,out=10,relative]
({(360/\nchilds*\xi)+10}:1.45cm) -- ({(360/\nchilds*\xi)-10}:1.45cm) to [in=170,out=20,relative] ({(360/\nchilds*\xi)-3}:3.2cm);
\node[regular polygon, regular polygon sides=6,fill=red,minimum width=2cm,shape border uses incircle,shape border rotate=(360/\nchilds*\xi)] at (360/\nchilds*\xi:4cm) {\x};
}
\end{tikzpicture}
\end{document}
(many thanks to #marmot for the hint about shape border rotate!)

Improving outputs in tikz package

$F_1(g)=\left\{
\begin{tikzpicture}
\node[shape=circle,draw=black] (A) {$b^1_0$};
\end{tikzpicture} \right\}$
Doing this code, I have an output like this Output
And I'd like something more centered into the brackets, like if I'm still writing instead of drawing.
You have to change the baseline of the picture to be at its center, then align that baseline to the vertical center of the text. This can be done with the option [baseline={([yshift=-.8ex]current bounding box.center)}]. I added it to the example code you gave, and also changed the inner sep of the circle to 0pt. Let me know if that works for you.
$F_1(g)=\left\{
\begin{tikzpicture}[baseline={([yshift=-.8ex]current bounding box.center)}]
\node[shape=circle,draw=black, inner sep=0pt] (A) {$b^1_0$};
\end{tikzpicture} \right\}$
\end{document}

How to only round selected corners in a fancytitle box with Tikz

If you take a look at http://www.texample.net/tikz/examples/boxes-with-text-and-math/ the boxes there are with rounded corners. In the examples, both the box itself and the title is a box. I want the title box to not have the bottom corners rounded.
On page 120 in the manual, there is a description of how to draw with and without rounded corners. However, I want to use this in a fancytitle. It looks a bit silly to have the fancytitle as a box where all corners are rounded when it is as wide as the box itself.
\begin{tikzpicture}[baseline=-2cm]
\node [mybox] (box){
\begin{minipage}[t!]{0.50\textwidth}
Help, I'm a box
\end{minipage}
};
\node[fancytitle, text width=0.5423\textwidth, text centered, rounded corners] at (box.north) {Help, I'm a title};
\end{tikzpicture}
The style I use is this
\tikzstyle{mybox} = [draw=red, fill=blue!20, very thick,
rectangle, rounded corners, inner sep=10pt, inner ysep=20pt]
\tikzstyle{fancytitle} = [fill=red, text=white]
Possibly the most simple way to achieve the effect (with out solving the problem) is as follows.
Add name=title to the title node.
then draw a line along the bottom of the title node.
\draw [draw=red,line width=2pt] (title.south west) -- (title.south east);
This gives two little spots where the line over shoots, to fix this you can add.
\usetikzlibrary{calc}
And move make the line 1 point shorter at each end, and up a bit.
\draw [draw=red,line width=2pt] ($(title.south west)+(+1pt,+1pt)$) -- ($(title.south east)+(-1pt,+1pt)$) ;

Resources