I use the following code to create a box for equations. It uses Tikz to create the box.
\documentclass[a4paper]{article}
\usepackage{tikz}
\usepackage{amsmath}
\usepackage{amsmath,amssymb}% pour les maths
\usepackage{enumitem}
\usepackage{varwidth}
\usepackage{listings}
\usetikzlibrary{calc}
\newcommand{\mybox}[4][\textwidth-\pgfkeysvalueof{/pgf/inner xsep}-2mm]{%
\begin{figure}[!h]
\centering
\begin{tikzpicture}
\node[line width=.5mm, rounded corners, draw=#2, inner ysep=10pt, text width=#1, outer sep=0] (one) {\vspace*{15pt}\\\begin{varwidth}{\textwidth}#4\end{varwidth}};
\node[text=white,anchor=north east,align=center, minimum height=20pt] (two) at (one.north east) {#3 \hspace*{.5mm}};
\path[fill=#2]
(one.north west|-two.west) --
($(two.west)+(-1.5cm,0)$)
to[out=0,in=180] (two.south west) --
(two.south east) [rounded corners] --
(one.north east) --
(one.north west) [sharp corners] -- cycle;
\node[text=white,anchor=north east,align=center, minimum height=20pt, text height=2ex] (three) at (one.north east) {#3 \hspace*{.5mm}};
\end{tikzpicture}
\end{figure}
}
\begin{document}
\mybox{green!70!black}{The Caption}{
\begin{enumerate}
\item Show that\\
$\displaystyle D_2f(x,y) = \frac{\partial {}}{\partial{y}} \biggl( \int_0^xg_1 (t,0) \ dt + \int_0^y g_2(x,s) \ ds \biggr)$
\item prove that\\
$\displaystyle \biggl(\forall x\in\mathbb{R} \biggr)\biggl(\forall y \in \mathbb{R} \biggr) x\neq y\, \text{ and } \, x+y \neq 2 \implies x^{2}-2x \neq y^2-2y$
\end{enumerate}
}
\end{document}
but Now I want to put listing inside the box
When I put the listing inside the mbox I got an error
\begin{lstlisting}[language=R]
fun1 <- function(data, data.frame, graph=TRUE, limit=20, ...) {
[omitted statements]
if (graph)
par(pch="*", ...)
[more omissions]
}
\end{lstlisting}
the error is
! Argument of \lst#next has an extra }.
You cannot use listings inside arguments directly. You either need to escape and properly prepare the verbatim code or store it somewhere else. See section 6.1 of the listing manual for more information.
Store the listing in a box
\begin{document}
\begin{lrbox}{\mylisting}
\begin{lstlisting}[language=R]
fun1 <- function(data, data.frame, graph=TRUE, limit=20, ...) {
[omitted statements]
if (graph)
par(pch="*", ...)
[more omissions]
}
\end{lstlisting}
\end{lrbox}
\mybox{green!70!black}{The Caption}{
\usebox\mylisting
}
\end{document}
Store the listing in an external file
\lstinputlisting{external_file.R}
Add line feeds ^^J
\begin{lstlisting}[language=R]^^J
fun1 <- function(data, data.frame, graph=TRUE, limit=20, ...) {^^J
[omitted statements]^^J
if (graph)^^J
par(pch="*", ...)^^J
[more omissions]^^J
}^^J
\end{lstlisting}
Note: You will probably need to resize your box for that long first line of code.
Related
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}
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}
Currently, I'm working in LaTeX and want to make labels with a for-loop. For a certain reason I want to use the for-loop which goes from 4 to 7.
\documentclass{report}
\usepackage{circuitikz}
\begin{document}
\begin{circuitikz}
\foreach \i in {4,...,7}
{
\draw(0, -\i) node[anchor=north west] {Elektrode\textsubscript{\i-4} +};
}
\end{circuitikz}
\end{document}
What I want is that it will output: Elektrode0; Elektrode1, Elektrode2, Elektrode3
But what I get is: Elektrode4-4; Elektrode5-4, Elektrode6-4, Elektrode7-4.
What am I doing wrong?
There are many possible approaches, but one easy workaround is to use a second variable:
\documentclass{report}
\usepackage{circuitikz}
\begin{document}
\begin{circuitikz}
\foreach[count = \x from 0 ] \i in {4,...,7}
{
\draw(0, -\i) node[anchor=north west] {Elektrode\textsubscript{\x} +};
}
\end{circuitikz}
\end{document}
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 could I center the two lines inside the brackets. The code so far is:
\documentclass[a4paper,12pt,oneside]{report}
\usepackage{amsmath}
\begin{equation}
x_{t}^{\ast }(n)=
\left\{\!\begin{aligned}
& \hfil {x_{t}}\\
\hfil & {0,\text{ otherwise}}
\end{aligned}\right\}
\end{equation}
Here are a couple of options:
\documentclass{article}
\usepackage{amsmath,eqparbox,xparse}
% https://tex.stackexchange.com/a/34412/5764
\makeatletter
\NewDocumentCommand{\eqmathbox}{o O{c} m}{%
\IfValueTF{#1}
{\def\eqmathbox###1##2{\eqmakebox[#1][#2]{$##1##2$}}}
{\def\eqmathbox###1##2{\eqmakebox{$##1##2$}}}
\mathpalette\eqmathbox#{#3}
}
\makeatother
\begin{document}
\[
\renewcommand{\arraystretch}{1.2}
x_t^* (n) =
\left\{\begin{array}{#{} c #{}}
x_t \\
0, \text{ otherwise}
\end{array}\right\}
\]
\[
x_t^* (n) =
\left\{\begin{aligned}
\eqmathbox[cond]{x_t} \\
\eqmathbox[cond]{0, \text{ otherwise}}
\end{aligned}\right\}
\]
\end{document}
The first uses an array with a single, centred column. The second uses aligned and sets each entry inside an \eqmathbox[<tag>] with the same <tag>. eqparbox then finds the maximum width of those elements with the same <tag>, and sets each element inside a box of that width (default <align>ment is centred \eqmathbox[<tag>][<align>]{<stuff>}).