Latex – Typesetting

latex.png

This part of my page provides a cookbook with some functionalities in \LaTeX. There are some commands and configurations that I found useful at some point. You also find here a .sty file that contains Mathematical annotations that I use. The mathematical annotation for latex provided by the American Mathematical Society is very rich, but lack some very simple commands that are recurrent for my usage, as iid random variable annotation, or expectation and variance of random variables. I created that sty file that have those commands to facilitate my typesetting using \LaTeX. You can find the .sty file and the instructions here. The way I particularly use it in conjunction with emacs, org-mode, and snippets to make typesetting in \LaTeX faster and more efficient are described in my workflow.


My Mathematical Commands for Latex

To use these mathematical annotation, you just have to save the file my-math-commands.sty that you find here in the folder you have your .tex file. The commands are describe in the .sty file itself. Some examples are commands for writing operators of argmax, argmin, variance, covariance, correlation, iid random varialble.

Note: I keep changing and improving that file. If you have suggestions, let me know

Text formating

Justification

Align text at the right and left in the same line

Problem: write part of the line aligned in the left, and after some point, align the line at the right (yes, I can’t remember why I found it useful one day!).

Solution: Vide example:

Complete example

\documentclass{article}

\begin{document}
Occasionally you may want to have text right-aligned in a LaTeX document. Other times you may want a block of left-aligned text next to a block of right-aligned text. LaTeX provides this functionality with the 
\hfill keyword. \hfill is a horizontal fill keyword. It tells LaTeX that you want to expand the space between the text on the right (if any) and the text on the left (if any) to the maximum width. In other words, if itís the first thing on the line then that line will be right justified, otherwise the preceding text will be left justified and any text afterwards will be right-justified. Letís see an example.

\hfill All of this text

\hfill is right justified

\hfill but we need a blank line

\hfill between each line

Will produce:
All of this text
is right justified
but we need a blank line
between each line

Left block \hfill Right block\\

This is still on the left \hfill This is still on the right

Will produce:

Left block                                Right block

This is still on the left                 This is still on the right

\end{document}

Go to Table of Content

Change enumeration

The package enumitem allows to change the character of enumeration environment. Here are two examples.

\documentclass{article}
\usepackage{enumitem,xcolor}
% for enumeration using index
 \newcommand{\subscript}[2]{$#1 _ #2$}
\begin{document}


\begin{enumerate}[label=\color{blue}\theenumi]
 %  \begin{enumerate}[\color{blue} 1]% Alternative
\item First
\item Second
\item Third
\end{enumerate}

\begin{enumerate}[label=\color{red}(A\arabic*)]
\item First
\item Second
\item Third
\end{enumerate}

\begin{enumerate}[label=(\subscript{I}{\arabic*})]
\item First
\item Second
\item Third
\end{enumerate}

\end{document}

Equations

Multiline underbrace and normal size of equation inside table

Problem 1: First goal is to keep the size of the equation inside other environment, as tables, itemize, inline equations. By default, latex shrinks the fractions, for instance.

Solution: \displaystyle

Example: in a table

Description Latex Code Result
equation is (no displaystyle): $\frac{1}{2}$ \frac{1}{2}
equation is (w/ displaystyle): $displaystyle \frac{1}{2}$ \displaystyle \frac{1}{2}

Problem 2: be able to write comment in a part of the equation in multiple lines

Solution: \substack in the package amsmath

Example:

With no \substack, the code

\[
\(\displaystyle 
\frac{\partial F_i}{\partial I_i} + 
\underbrace{\frac{\partial F_i}{\partial K_i}\frac{\partial
K_i}{\partial I_i}}_{\text{Indirect effect of}
I_i  \text{due to inflow of capital} } 
\]

Produces

\displaystyle \frac{\partial F_i}{\partial I_i} + \underbrace{\frac{\partial F_i}{\partial K_i}\frac{\partial K_i}{\partial I_i}}_{\text{Indirect effect of} I_i  \text{due to inflow of capital} }

With \substack, the code

\[
\displaystyle 
\frac{\partial F_i}{\partial I_i} + 
\underbrace{\frac{\partial F_i}{\partial K_i}\frac{\partial
K_i}{\partial I_i} }_{\substack{\text{Indirect effect of}\\ \text{ $
I_i $  due to inflow of capital  } }}
\]

Produces (maybe html does not display it)

\displaystyle \frac{\partial F_i}{\partial I_i} + \underbrace{\frac{\partial F_i}{\partial K_i}\frac{\partial K_i}{\partial I_i} }_{\substack{\text{Indirect effect of}\\ \text{ I_i $ due to inflow of capital } }} $

Complete example

\documentclass[a4paper]{article}
\usepackage{amsmath}

\begin{document}

\begin{itemize}
	 \item \$ $\backslash$displaystyle ... \$: keep the original equation font size and not compress fractions
	   \item \$ $\backslash$substack:  ... \$ :allow you to write text under equation in multiple lines
\end{itemize}

\subsubsection*{Example of display style}

\begin{itemize}
	\item some in line fraction $\frac{1}{1}$ without display style
	\item some in line fraction $\displaystyle \frac{1}{1}$ with display style
\end{itemize}
\subsubsection*{Example of substack (with display style)}

In line equation  $ \displaystyle 
\frac{\partial F_i}{\partial I_i} + 
\underbrace{\frac{\partial F_i}{\partial K_i}\frac{\partial K_i}{\partial I_i} }_{\substack{\text{Indirect effect of}\\ \text{ $ I_i $  due to inflow of capital  } }}$ and more text

\end{document}

Go to Table of Content

Theorems, Definitions and the like

Add box around theorem, add summary with list of theorems, definitions etc, add and extra margin for that list

Problem 1: Create a box for definitions, theorems, etc.

Solution: vide example

Problem 2: create a list of definitions, theorems, with sufficient margin in the list of theorems if the theorem is numbered in subsubsections.

Solution: vide example

\documentclass{article}
\usepackage{amsthm}
\usepackage{thmtools}
\usepackage{xcolor}

% set up box around theorem
\declaretheoremstyle[
			spaceabove=6pt, 
			spacebelow=6pt,
			headfont=\normalfont\bfseries,
			notefont=\mdseries, 
			notebraces={(}{)},
			bodyfont=\normalfont\itshape,
			postheadspace=\newline,
			numberwithin=subsubsection,
			shaded={rulecolor=black,
						rulewidth=.4pt, 
						bgcolor=gray!20}]{defStyle}
\declaretheorem[name=Definition,style=defStyle]{definition}
%=================================================

% add extra margin on the list of theorems
\makeatletter
\renewcommand\thmt@listnumwidth{3.5em}
\makeatother

\begin{document}
\listoftheorems


\section{First}
\subsection{First}
\subsubsection{First}
\subsubsection{Second}
\begin{definition}[Test]
Test
\end{definition}

\end{document}

Go to Table of Content

Box around head of theorem

This solution is due to latex community website. It draws a box around the head of the theorem, and you can include the name of the theorem in or out of the box.

\documentclass{article}
\usepackage{amsthm}
\usepackage{tikz}

\makeatletter
\newtheoremstyle{style1}
  {\topsep}
  {\topsep}
  {\itshape}
  {}
  {\bfseries}
  {}
  {.5em}
  {\tikz[baseline=-.65ex]\node[draw,rounded corners] {\thmname{#1}~\thmnumber{#2}\@ifempty{#3}{.}{}};\thmnote{ (#3).}}
\makeatother
\theoremstyle{style1}
\newtheorem{theo}{Theorem}

\makeatletter
\newtheoremstyle{style2}
  {\topsep}
  {\topsep}
  {\itshape}
  {}
  {\bfseries}
  {}
  {.5em}
  {\tikz[baseline=-.65ex]\node[draw] {\thmname{#1}~\thmnumber{#2}\@ifempty{#3}{.}{}\thmnote{ (#3).}};}
\makeatother
\theoremstyle{style2}
\newtheorem{theo2}{Theorem}


\begin{document}

\begin{theo}
test
\end{theo}

\begin{theo}[Important result]
test
\end{theo}

\begin{theo2}[Important result 2]
test 2
\end{theo2}

\end{document}

Bibliography

Add bibliography in the body of the text

Problem: create a full citation inside the text, not only the name-of-authors (year).

Solution: \usepackage{bibentry}. See example

\begin{filecontents}{mybib.bib}
	@book{example,
		author = "John",
		title = "The book's title",
		year = "2013",
		publisher = "Cambridge",
	}

\end{filecontents}

\documentclass{article}
%\usepackage[round]{natbib} % bibliography package
\usepackage{bibentry}       %  full citation in the body of the text (turn off natbib if using it)
\nobibliography*            % no bib at the end


\begin{document}

This would be the complete citation
\bibentry{example}
And this would be just the regular citation: \cite{example}



\bibliography{mybib}
\bibliographystyle{apalike}

\end{document}

Go to Table of Content

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s