Create new variable each time an environment in LaTeX is called - latex

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}

Related

Koma scrbook seems not to include BCOR

The proof copies of a book laid out with KOMA scrbook show that the inner margins are too narrow. I had BCOR set to 10mm and want to increase them, but my experiments with BCOR set to 20mm show that the layout is the same independent what BCOR value I set. Here my MWE:
\documentclass{scrbook}
\KOMAoptions{paper=
128.5mm:198.4mm, %(5,06" x 7,91") %ziel
BCOR=20mm,
twoside,
headinclude=false, footinclude=false,
headings=normal,
titlepage=true,
% draft=true,
DIV=9, %ziel kleines buch
fontsize=12pt,
}
\usepackage[german]{babel}
\usepackage{lipsum}
\usepackage{geometry}
\usepackage[a4,axes,cross,pdftex,center]{crop}
\begin{document}
\frontmatter
\tableofcontents
\mainmatter
\part{ Philosophie}
some text
\lipsum
\lipsum
\end{document}
What is wrong?
My real question is: how can I just move the text area on both pages to the outside (i.e. making the gutter wider) without changing the text area size (and thus preserve line breaks)?
I found a hint in some other questions answered before and the problem was caused by the interaction between the KOMA script and the geometry package. Using the geometry package recalculates the page layout and overwrites the one calculated by scrbook including the BCOR value. This can be avoided when adding the option pass to geometry as in
`\usepackage[pass]{geometry}`.
In general the interaction between the layout calculated by KOMAscript and other package can lead to surprises. The discussion in https://tex.stackexchange.com/questions/182821/div-and-bcor-setting-in-koma-best-practices/183839 is very useful.

How to prevent floats form moving between a paragraph and an align?

In my document I talk about a calculation and then I do all the equations. So I would like to have those next to each other. Unfortunately some floats really make life difficult.
I have the folowing latex code:
[Some Paragraph]
\begin{table}
... This is a small table (4rows)
\end{table}
\begin{table}
... This table spans almost a page
\end{table}
\begin{figure}
... This figure spans half a page
\end{figure}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
THIS PARAGRAP REFERS TO THE CALCULATION BELOW AND SHOULD BE FOLLOWED IMMEDIATELY BY THE CALCULATION.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{align*}
... This is the CALCULATION.
\end{align*}
The result is that all the tables and figures slide between the PARAGRAPH and the CALCULATION.
I could fix every Figure with the [H] parameter but that's not what I really want to do. Instead I want to prevent anything from sliding between my PARAGRAPH and my CALCULATION
How is it possible to prevent floats from getting between a paragraph and an align?

Caution Statement in LaTex?

Question:
I use caution statements in my LaTex documents using the code below which works, but I need the caution symbol to be centre aligned against the text like below image that shows current and expected. How?
\newlist{Caution}{enumerate}{1}
\setlist[Caution]{label=\scshape{\color{black}\colorbox{yellow}{{\textbf{{\faWarning} caution}}}},leftmargin=*}
\begin{Caution}
\item {A hazardous situation which, if not avoided, could result in minor or moderate njury.}
\end{Caution}
Edit 1: Minimum Reproducible Example
\documentclass[11pt,a4paper]{article}
\usepackage[a4paper,bindingoffset=0mm,left=20mm,right=20mm,%
top=30mm,bottom=20mm,footskip=10mm,headsep=10mm]{geometry}
\usepackage[utf8]{inputenc}
\usepackage{palatino}
\usepackage{enumitem}
\usepackage{xcolor, fontawesome}
\usepackage{multicol}
\twocolumn
\newlist{Caution}{enumerate}{1}
\setlist[Caution]{label=\scshape{\color{black}\colorbox{yellow}{{\textbf{{\faWarning} caution}}}},leftmargin=*}
\begin{document}
\begin{Caution}
\item {A hazardous situation which, if not avoided, could result in minor or moderate injury.}
\end{Caution}
\end{document}
You can control the vertical position of any box with the command \raisebox and this can be used to raise or lower the label. But it is not a very good idea for your problem as the amount that the label must be lowered depends on the number of lines in the item text. More, it is not very easy to pass optional positioning parameters to a label, and this solution really lacks flexibility, but I included it for comparisons and completeness.
I propose another solution with minipages. It is sufficient to put the caution label and the text in minipages and to specify a center ([c]) vertical alignment.
The width of either parts are computed with the calc package and it adapts to font size modifications. There is an optional argument that allows to control the spacing between the label and the text. Default value of this parameter mimics the spacing in a list, but it can be adjusted if required.
\documentclass[11pt,a4paper]{article}
\usepackage[a4paper,bindingoffset=0mm,left=20mm,right=20mm,%
top=30mm,bottom=20mm,footskip=10mm,headsep=10mm]{geometry}
\usepackage[utf8]{inputenc}
\usepackage{palatino}
\usepackage{enumitem}
\usepackage{xcolor, fontawesome}
\usepackage{multicol}
\usepackage{calc}
\twocolumn
\newcommand{\cautionmark}{\scshape{\color{black}\colorbox{yellow}{\faWarning caution}}}
\newlist{Caution}{enumerate}{1}
\setlist[Caution]{label=\raisebox{-0.5cm}[0pt][0pt]{\cautionmark},leftmargin=*}
\newcommand{\newcaution}[2][0.7em]{
\begin{minipage}[c]{\widthof{\cautionmark}}
\cautionmark
\end{minipage}%
\hfill%
\begin{minipage}[c]{\linewidth-\widthof{\cautionmark}-#1}
#2
\end{minipage}
}
\begin{document}
\begin{Caution}
\item A hazardous situation which, if not avoided, could result in
minor or moderate injury.
\end{Caution}
\newcaution{A hazardous situation which, if not avoided, could result
in minor or moderate injury.}
\newcaution[1cm]{A hazardous situation which, if not avoided, could result
in minor or moderate injury.}
\end{document}

How to center LaTeX xtable (And Figure) output in full text width

This is a follow-up to a question I posted earlier (How to center LaTeX xtable output in full text width).
I realize that my MWE from this previous post was incomplete. In an effort to make it as minimal of an example as possible, I did leave out something that ended up conflicting. Hence, here, I am posting the issue more fully.
I am using tufte-handout (http://mirrors.ibiblio.org/CTAN/macros/latex/contrib/tufte-latex/sample-handout.pdf) to create a small report in latex. I have a file code.Rnw that I knit into code.tex. Below is my code.Rnw:
\documentclass[12pt,english,nohyper]{tufte-handout}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{longtable}
\usepackage{wrapfig}
\usepackage{hyperref}
\usepackage{graphicx}
\usepackage[space]{grffile}
\usepackage{geometry}
\usepackage{pgffor}
\usepackage{calc}
\usepackage{enumitem}
\usepackage{microtype}
\usepackage{tabularx}
%\usepackage{floatrow}
\begin{document}
<<include=FALSE>>=
library(ggplot2)
library(xtable)
#
\begin{fullwidth}
\makeatletter\setlength\hsize{\#tufte#fullwidth}\makeatother
<<echo=FALSE,results='asis'>>=
x.big <- xtable(mtcars[1:10,1:4], label ='tab:mtcars',caption ='This is the mtcar dataset head.',align = c("rr|lr|r"))
print(x.big, tabular.environment ='longtable', floating = FALSE, include.rownames=FALSE)
#
\end{fullwidth}
<<echo=FALSE,results='asis'>>=
fnameO <<- "plot.pdf"
pdf(paste0("./",fnameO),width=6,height=7)
print(qplot(hp, mpg, data=mtcars, main="Scatterplots of MPG vs. Horsepower", xlab="Horsepower", ylab="Miles per Gallon"))
{dev.off();invisible()}
#
\begin{fullwidth}
\makeatletter\setlength\hsize{\#tufte#fullwidth}\makeatother
\begin{figure}[!ht]
\includegraphics[width=\linewidth]{\Sexpr{fnameO}}
\caption{This is a plot of the mtcars dataset from R. It compares the horsepower with the miles per gallon. It uses the qplot function from ggplot2.}
\label{fig:LearningObj_summary}
\end{figure}
\end{fullwidth}
\end{document}
This is the output:
I am desiring to have both the table and the figure centered (across the whole page). As shown above, I am successfully able to get the table centered (thanks to advice from a user in my previous post).
However, I am unable to get the figure centered across the whole page with the caption below it. Instead, likely due to the document class I am using (tufte-handout), the figure itself is in the non-margin area, and its caption is in the margin area.
For starters, I uncommented the \usepackage{floatrow} in the code, in an attempt to force the figure caption to be below the figure instead of to the right of it. This lead to an output as such (where both the table and figure are undesirably on the left side instead of centered, but the figure caption is indeed below the figure):
My question is: How can I center both the table and figure (with the caption below it), so that the output would look more like this?:
Thank you.
As a crude hack, you could simply modify the figure environment of your MWE to use the original \caption command:
\begin{fullwidth}
\begin{figure}[!ht]
\makeatletter\setlength\hsize{\#tufte#fullwidth}\setlength\linewidth{\#tufte#fullwidth}\let\caption\#tufte#orig#caption\makeatother
\includegraphics[width=\linewidth]{\Sexpr{fnameO}}
\caption{This is a plot of the mtcars dataset from R. It compares the horsepower with the miles per gallon. It uses the qplot function from ggplot2.}
\label{fig:LearningObj_summary}
\end{figure}
\end{fullwidth}
...or, for a bit smaller figure that is centered on the page:
\begin{fullwidth}
\begin{figure}[!ht]
\makeatletter\setlength\hsize{\#tufte#fullwidth}\setlength\linewidth{\#tufte#fullwidth}\let\caption\#tufte#orig#caption\makeatother
\centering
\includegraphics[width=.4\linewidth]{\Sexpr{fnameO}}
\caption{This is a plot of the mtcars dataset from R.}
\label{fig:LearningObj_summary}
\end{figure}
\end{fullwidth}
If you find the original \caption command lacking and if none(!) of the floats in your document need to use tufte captions you can overwrite the \caption command using something like \usepackage[labelfont=bf,compatibility=false]{caption}.

Tikzpicture on every page

How can I include a tikzpicture on every page?
I would like to create a complicated document template (page should be framed, and have a table to hold document information both in the header and footer).
I was thinking of using something like:
\begin{tikzpicture}[remember picture,overlay]
% complicated layout should be here, simple example is given below:
% \node [xshift=1cm,yshift=1cm] at (current page.south west)
% {This is an absolutely positioned text in the page};
\end{tikzpicture}
Do you have any other suggestions on how to create such a template?
Add information to header/footer either using fancyhdr Or KOMA Script
For adding a something on every page I used this:
\usepackage{eso-pic}
\makeatletter
\AddToShipoutPicture{%
\setlength{\#tempdimb}{.1\paperwidth}%
\setlength{\#tempdimc}{.04\paperheight}%
\setlength{\unitlength}{1pt}%
\put(\strip#pt\#tempdimb,\strip#pt\#tempdimc){%
\makebox(0,0){ \textcolor{gray}{Rev: \svnrev{} (\svnfilerev)} }%
}%
}
\makeatother
Here, I add the SVN revision number on the bottom right of every page. I don't remember why I did not use a tikzpicture[overlay,remember picture] in \AddToShipoutPicture, maybe because it can't remember the picture position in the state of shipping out the page.
Hope that helps.

Resources