Conditional selection of tikz style classes - latex

I try to achieve that different style classes are chosen in a self-defined tikz function depending on the input number.
However, the \ifnum command doesn't seem to work as I expect it.
The error message that I get is:
> thesis/image/outline_MWE.tex:46: Missing = inserted for \ifnum.
<to be read again>
}
l.46 \makeoutlinefig{1}
thesis/image/outline_MWE.tex:46: Missing number, treated as zero.
<to be read again>
}
l.46 \makeoutlinefig{1}
MWE:
\documentclass[varwidth=true]{standalone}
% \input{../preamble.tex}
% \input{../colors.tex}
\usepackage{tikz}
\usetikzlibrary{shapes,arrows,arrows.meta, calc, decorations.markings, backgrounds,fit,positioning,plotmarks, intersections, patterns, intersections,decorations.text,external,decorations.pathreplacing}
\begin{document}
\DeclareRobustCommand{\makeoutlinefig}[1]{
\centering
\def\threebw{16.7cm}
\begin{tikzpicture}
[auto, box/.style ={rectangle,
% font= \tiny,
draw=black,
thick,
% fill=blue!30,
text width=3.0cm,
minimum width=1.5cm,
align=center,
rounded corners,
minimum height=2.0cm,
dashed, thick},
activebox/.style = {box, draw=red,
thick,solid},
node distance = 0.5cm,
]
\node[style=\ifnum#1=1 activebox\else box\fi,
text width=\threebw] (b1) at (0,0) {\textbf{1. Chapter}};
\node[style=\ifnum#1=2 activebox\else box\fi,
text width=\threebw, below = of b1] (b2) {\textbf{2. Chapter}} ;
\draw [-Stealth,ultra thick] (b1) -- (b2);
\end{tikzpicture}
}
\makeoutlinefig{1}
\end{document}

You can avoid the problem like this:
\documentclass[varwidth=true]{standalone}
\usepackage{tikz}
\usetikzlibrary{shapes,arrows,arrows.meta, calc, decorations.markings, backgrounds,fit,positioning,plotmarks, intersections, patterns, intersections,decorations.text,external,decorations.pathreplacing}
\begin{document}
\DeclareRobustCommand{\makeoutlinefig}[1]{
\centering
\def\threebw{16.7cm}
\begin{tikzpicture}[
auto,
box/.style = {
rectangle,
draw=black,
thick,
text width=3.0cm,
minimum width=1.5cm,
align=center,
rounded corners,
minimum height=2.0cm,
dashed,
thick
},
activebox/.style = {
box,
draw=red,
thick,
solid
},
node distance = 0.5cm,
]
\ifnum#1=1
\def\mystyle{activebox}
\else
\def\mystyle{box}
\fi
\node[\mystyle, text width=\threebw] (b1) at (0,0) {\textbf{1. Chapter}};
\ifnum#1=2
\def\mystyle{activebox}
\else
\def\mystyle{box}
\fi
\node[\mystyle, text width=\threebw, below = of b1] (b2) {\textbf{2. Chapter}} ;
\draw [-Stealth,ultra thick] (b1) -- (b2);
\end{tikzpicture}
}
\makeoutlinefig{2}
\end{document}

Related

Latex pgfplots cannot show the line

I am trying to plot a graph like this in Latex:
It turns out to be looking like this with out a line:
This is my tex file
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{pgfplots}
\pgfplotsset{compat=1.5}
\begin{document}
\begin{center}
\begin{tikzpicture}
\begin{axis}[
axis lines = left,
ytick={0,0.25},
xtick={0,2,4,6,8,10},
height=5.5cm,
width=13cm,
xmax=10,
ymax=0.25,
ymin=0,
xmin=0,
xlabel = T,
ylabel = k,
]
\addplot [
domain=0:100,
samples=1000,
color=blue,
]
{0.001^(x/x+1) * (1+x)};
\addlegendentry{\(T=p^{\frac{k}{k+1}(k+1)}\)}
\end{axis}
\end{tikzpicture}
\end{center}
\end{document}
Have I done any thing wrong? Thank you.
Simple math problem: (x/x+1) != (x/(x+1))
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{pgfplots}
\pgfplotsset{compat=1.5}
\begin{document}
\begin{center}
\begin{tikzpicture}
\begin{axis}[
axis lines = left,
height=5.5cm,
width=13cm,
xlabel = T,
ylabel = k,
]
\addplot [
domain=0:10,
samples=100,
color=blue,
]
{ (1+x)*0.001^(x/(x+1)) };
\addlegendentry{\(T=p^{\frac{k}{k+1}}(k+1)\)}
\end{axis}
\end{tikzpicture}
\end{center}
\end{document}

Latex: \closedcycle Package pgf Warning: No path specified that can be filled on input

In the following plot, I am getting a warning. I know it has to do something with the coordinates but can't solve it for sure. If anyone can guide me in the right direction, it would be really helpful.
Package pgf Warning: No path specified that can be filled on input
line 55.
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usepackage{float}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\title{StackExchange}
\author{M. Tahasanul Ibrahim}
\date{January 2022}
\begin{document}
\begin{figure}[H]
\centering
\begin{tikzpicture}
\begin{axis}
[
% ybar,
% xmin=-1,xmax=10,
% ymin=0,ymax=18,
% xlabel={Data Value},
% ylabel={Occurrence/Frequency}]
xlabel= {Data Value},
ylabel= {Occurrence/Frequency},
enlarge x limits=0.1,
legend style={
at={(0.5,-0.15)},
anchor=north,legend columns=-1
},
width=12.8cm,
height=8cm,
point meta={x*100},
symbolic x coords={0,1,2,3,4,5,6,7,8,9},
grid=both,
grid style={line width=.1pt, draw=gray!10},
major grid style={line width=.2pt,draw=gray!50},
% axis lines=middle,
minor tick num=5,
nodes near coords={$\pgfmathprintnumber{\pgfkeysvalueof{/data point/y}}$},
]
\addplot+ [color=black, bottom color=green, top color=red] coordinates
{
(0,1)
(1,5)
(2,7)
(3,12)
(4,15)
(5,9)
(6,7)
(7,3)
(8,0)
(9,1)
} \closedcycle;
\end{axis}
\end{tikzpicture}
\caption{Area chart representing statistical data}
\end{figure}
\end{document}
These warnings arise because you are not only filling this path, you are also drawing the markers and the label markers in the same step.
If you decompose this into two separate steps, you can avoid the warnings:
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usepackage{float}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\title{StackExchange}
\author{M. Tahasanul Ibrahim}
\date{January 2022}
\begin{document}
\begin{figure}[H]
\centering
\begin{tikzpicture}
\begin{axis}
[
% ybar,
% xmin=-1,xmax=10,
% ymin=0,ymax=18,
% xlabel={Data Value},
% ylabel={Occurrence/Frequency}]
xlabel= {Data Value},
ylabel= {Occurrence/Frequency},
enlarge x limits=0.1,
legend style={
at={(0.5,-0.15)},
anchor=north,legend columns=-1
},
width=12.8cm,
height=8cm,
point meta={x*100},
symbolic x coords={0,1,2,3,4,5,6,7,8,9},
grid=both,
grid style={line width=.1pt, draw=gray!10},
major grid style={line width=.2pt,draw=gray!50},
% axis lines=middle,
minor tick num=5,
% nodes near coords={$\pgfmathprintnumber{\pgfkeysvalueof{/data point/y}}$},
]
\addplot [color=black, bottom color=green, top color=red] coordinates
{
(0,1)
(1,5)
(2,7)
(3,12)
(4,15)
(5,9)
(6,7)
(7,3)
(8,0)
(9,1)
} \closedcycle;
\addplot+[black,mark=*,nodes near coords={$\pgfmathprintnumber{\pgfkeysvalueof{/data point/y}}$},] coordinates
{
(0,1)
(1,5)
(2,7)
(3,12)
(4,15)
(5,9)
(6,7)
(7,3)
(8,0)
(9,1)
};
\end{axis}
\end{tikzpicture}
\caption{Area chart representing statistical data}
\end{figure}
\end{document}

Graph in Latex using Tikz and PGFplots

How to draw this graph in LaTeX? I have used TikZ but I can't get it done exactly.
This is what I have tried so far:
\begin{tikzpicture}
\tkzInit[xmax=6,ymax=6,xmin=-6,ymin=-1]
\tkzAxeXY
\draw[thick] (6,3) -- (-4,5);
\filldraw[black] (6,3) circle (2pt) node[anchor=west] {A(6,3)};
\filldraw[black] (-4,5) circle (2pt) node[anchor=east] {B(-4,5)};
\end{tikzpicture}
Using regular pgfplots you could do the following: Draw the line with the points you choose and label them with nodes. Then draw the additional distance markers using \draw in the axis coordinate system (The concrete positions of all elements may have to be adjusted).
\documentclass{article}
\usepackage{tikz}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\tiny
\begin{axis}[
width = 6cm,
height = 4cm,
ymajorgrids = true,
ymin=-1,
ymax=7,
xmin=-6,
xmax=6,
xtick distance=1,
ytick distance=1,
axis y line*=center,
axis x line*=center,
enlarge x limits=.12
]
\addplot+[sharp plot] coordinates {(6,3) (1,4) (-4,5) } node[below=1mm, pos=0] {A(6,3)}
node[pos=0.45,below=1mm] {P(x,y)}
node[pos=1,below=1mm] {B(-4,5)};
\draw[|-|] (axis cs:1.2,5) -- node [above, rotate=-10] {m=13} (axis cs:6.3,3.9);
\draw[|-|] (axis cs:-3.8,6) -- node [above, rotate=-10, pos=.3] {n=2} (axis cs:1.2,5);
\end{axis}
\end{tikzpicture}
\end{document}
Output:

Using Tikz positioning with \onslide\visible\only inside beamer

I have the following image:
and I want its elements to appear in a certain order in my beamer presentation. For the moment, I'm trying to make a_1, a_2 appear in a second slide. I'm using this code:
\documentclass{beamer}
\usepackage{textcomp}
\usepackage{tikz}
\usetheme{Madrid}
\begin{document}
\begin{frame}{}
\usetikzlibrary{shapes,arrows, positioning, calc}
\tikzset{%
block/.style = {rounded corners, draw, thick, circle, minimum height = 3em,
minimum width = 3em, fill = yellow!50},
point/.style = {coordinate}, % Input
}
\begin{tikzpicture}[auto, thick, node distance=2cm, >=triangle 45]
%\node[block] (A1) at (0,0) {$A_1$};
%\node[block, right = 1cm of A1] (A2) {$A_2$};
\node[below = of A1] (a1) {{\visible<2->{$a_1$}}};
\node[below = of A2] (a2) {{\visible<2->{$a_2$}}};
\node[below = of A1] (a1) {$a_1$};
\node[below = of A2] (a2) {$a_2$};
\draw[->] (A1.south) ++(0,-0.3) -- ++(0, -1.3);
\draw[->] (A2.south) ++(0,-0.3) -- ++(0, -1.3);
\end{tikzpicture}
\end{frame}
\end{document}
but all I get is this:
I get errors like: "unknown arrow tip kind 'triangle 45'" and also "unknown operator 'o' or 'of ' in (of A1).
It's my first time using Tikz, and I'm not really practical with beamer tools like \onslide, \only or \visible. I think I could create different images, one for each frame, and then add them with \includegraphics and \pause, but it would be more practical if I managed to achieve the same result without creating different pictures. Any help would be really appreciated.
load your tikz libraries in the preamble, not inside the frame
you can use the overlay-beamer-styles library to control the appearance of the nodes
\documentclass{beamer}
\usepackage{textcomp}
\usepackage{tikz}
\usetheme{Madrid}
\usetikzlibrary{shapes,arrows, positioning, calc}
\usetikzlibrary{overlay-beamer-styles}
\tikzset{%
block/.style = {rounded corners, draw, thick, circle, minimum height = 3em,
minimum width = 3em, fill = yellow!50},
point/.style = {coordinate}, % Input
}
\begin{document}
\begin{frame}{}
\begin{tikzpicture}[auto, thick, node distance=2cm, >=triangle 45]
\node[block] (A1) at (0,0) {$A_1$};
\node[block, right = 1cm of A1] (A2) {$A_2$};
\node[below = of A1, visible on=<2->] (a1) {$a_1$};
\node[below = of A2, visible on=<2->] (a2) {$a_2$};
\draw[->] (A1.south) ++(0,-0.3) -- ++(0, -1.3);
\draw[->] (A2.south) ++(0,-0.3) -- ++(0, -1.3);
\end{tikzpicture}
\end{frame}
\end{document}

How do I fix this tag positioning issue on TikZ?

I'm using Tikz to illustrate an idea. But I can't seem to align the tags as I would want. The code I'm using is this:
\usepackage[spanish]{babel}
\decimalpoint
\usepackage{fancyhdr} % Required for custom headers
\usepackage{lastpage} % Required to determine the last page for the footer
\usepackage{extramarks} % Required for headers and footers
\usepackage[usenames,dvipsnames]{color} % Required for custom colors
\usepackage{graphicx} % Required to insert images
\usepackage{listings} % Required for insertion of code
\usepackage{courier} % Required for the courier font
\usepackage{lipsum} % Used for inserting dummy 'Lorem ipsum' text into the template
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{natbib}
\usepackage{enumitem}
\usepackage{tikz}
\usetikzlibrary{babel}
\usetikzlibrary{positioning}
\begin{document}
\begin{tikzpicture}
\node (o) at (0,0) {};
\node (n1) at (2.1,1.1) {};
\node (n2) at (8.9,3.9) {};
\node (a1) at (2.1,0.3) {};
\node (a2) at (8.9,4.9) {};
\node (p1) at (2.1,0.1) {};
\node (p2) at (8.9,4.9) {};
\draw[->] (2,0) -- (9,0) node[at end, sloped, below] {$z$};
\draw[->] (2,0) -- (2,5) node[at end,left] {$u(z)$};
\draw (n1) -- (n2) node[midway, sloped, above] {Neutralidad};
\draw (a1) to [bend left=30] (a2) node[near end, sloped] {Propensión};
\draw (p1) to [bend right=35] (p2) node[midway, sloped] {Aversión};
\end{tikzpicture}
\end{document}
But the tags when I use the bend option appear one on top of the other and not in the position I want, they appear at the bottom left of the graph. Does anyone know how to fix this?
The node needs to be before the second coordinate:
\documentclass{article}
\usepackage[spanish]{babel}
\decimalpoint
\usepackage{fancyhdr} % Required for custom headers
\usepackage{lastpage} % Required to determine the last page for the footer
\usepackage{extramarks} % Required for headers and footers
\usepackage[usenames,dvipsnames]{color} % Required for custom colors
\usepackage{graphicx} % Required to insert images
\usepackage{listings} % Required for insertion of code
\usepackage{courier} % Required for the courier font
\usepackage{lipsum} % Used for inserting dummy 'Lorem ipsum' text into the template
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{natbib}
\usepackage{enumitem}
\usepackage{tikz}
\usetikzlibrary{babel}
\usetikzlibrary{positioning}
\begin{document}
\begin{tikzpicture}
\node (o) at (0,0) {};
\node (n1) at (2.1,1.1) {};
\node (n2) at (8.9,3.9) {};
\node (a1) at (2.1,0.3) {};
\node (a2) at (8.9,4.9) {};
\node (p1) at (2.1,0.1) {};
\node (p2) at (8.9,4.9) {};
\draw[->] (2,0) -- (9,0) node[at end, sloped, below] {$z$};
\draw[->] (2,0) -- (2,5) node[at end,left] {$u(z)$};
\draw (n1) -- (n2) node[midway, sloped, above] {Neutralidad};
\draw (a1) to [bend left=30] node[near end, sloped, above] {Propensión} (a2);
\draw (p1) to [bend right=35] node[midway, sloped,above] {Aversión} (p2);
\end{tikzpicture}
\end{document}

Resources