LaTeX - Environment tips

From XennisWiki
Jump to: navigation, search

Some tips for LaTeX environment. For general tips checkout the article LaTeX - Tips.

List

Unsorted list

\begin{itemize}
	\item One item
	\item A second item
\end{itemize}

Numbered list

\begin{enumerate}
	\item One item
	\item A second item
\end{enumerate}

Mathematics

Equation

\begin{equation}
	\mathrm{profitability}(j) = \frac{ \mathrm{gain}(j) }{ \mathrm{cost}(j) }
	\label{equ:oft}
\end{equation}

Align

The align environment is part of the amsmath package.

Example

\begin{align*}
	f(x) &= (x-2) \cdot 3 \\
	&= 3x-6
\end{align*}

Usage of &quad

\begin{align*}
	\max &\quad c^Tx\\
	\text{s.t.} &\quad Ax\leq b\\
	&\quad x\geq 0
\end{align*}

Usage of labels

\begin{align}
	f(x) &= x^2 + 3 \label{equ:quad} \\
	g(x) &= \frac{x}{4} \label{equ:frac}
\end{align}

Figure (include PNG, PDF, etc.)

Note: First \caption then \label

\begin{figure}
	\includegraphics[width=2.5in]{FILE}
	\caption{CAPTION}
	\label{fig:LABEL-NAME}
\end{figure}

Figure across multiple columns

Source: Bilder über zwei (mehrere) Spalten

Attach an * asterisk to figure.

\begin{figure*}
	\includegraphics[width=2.5in]{FILE}
	\caption{CAPTION}
	\label{fig:LABEL-NAME}
\end{figure*}

Subfigure

Source: LaTeX für Einsteiger - Grafiken nebeneinander

\usepackage{subfigure}
\begin{figure}
	\centering
	\subfigure[TITLE SUBFIGURE 1]{
		\label{LABEL 1}
		\includegraphics{FILE 1} 
	}
	\subfigure[TITLE SUBFIGURE 2]{
		\label{LABEL 2}
		\includegraphics{FILE 2} 
	}
	\caption{TITLE}
	\label{LABEL}
\end{figure}

Code

Include code (listings)

Further information: LaTeX/Packages/Listings

\usepackage{listings}

\definecolor{colorNavy}{HTML}{00CC33}
\definecolor{dkgreen}{RGB}{0,100,0}

\lstset{ %
  language=JAVA,                  % the language of the code
  basicstyle=\footnotesize,       % the size of the fonts that are used for the code
  numbers=left,                   % where to put the line-numbers
  stepnumber=2,                   % the step between two line-numbers. If it's 1, each line 
                                  % will be numbered
  numbersep=5pt,                  % how far the line-numbers are from the code
  frame=single,                   % adds a frame around the code
  rulecolor=\color{black},        % if not set, the frame-color may be changed on line-breaks
  tabsize=2,                      % sets default tabsize to 2 spaces
  captionpos=b,                   % sets the caption-position to bottom
  breaklines=true,                % sets automatic line breaking
  breakatwhitespace=false,        % sets if automatic breaks should only happen at whitespace
  keywordstyle=\color{blue},      % keyword style
  commentstyle=\color{dkgreen},   % comment style
  stringstyle=\color{colorNavy},  % string literal style
  morekeywords={*,with, where, from, union, all, as},
  extendedchars=true,
  literate={ä}{{\"{a}}}1 {ö}{{\"o}}1 {ü}{{\"u}}1,
}
Define another language

JavaScript

\lstdefinelanguage{JavaScript}{
  basicstyle=\tiny
  keywords={typeof, new, true, false, catch, function, return, null, catch, switch, var, if, in, while, do, else, case, break},
  keywordstyle=\color{blue}\bfseries,
  ndkeywords={class, export, boolean, throw, implements, import, this},
  ndkeywordstyle=\color{darkgray}\bfseries,
  identifierstyle=\color{black},
  sensitive=false,
  comment=[l]{//},
  morecomment=[s]{/*}{*/},
  commentstyle=\color{purple}\ttfamily,
  stringstyle=\color{red}\ttfamily,
  morestring=[b]',
  morestring=[b]",
}

XML

\definecolor{gray}{rgb}{0.4,0.4,0.4}
\definecolor{darkblue}{rgb}{0.0,0.0,0.6}
\definecolor{cyan}{rgb}{0.0,0.6,0.6}

\lstdefinelanguage{XML}
{
  morestring=[b]",
  morestring=[s]{>}{<},
  morecomment=[s]{<?}{?>},
  stringstyle=\color{black},
  identifierstyle=\color{darkblue},
  keywordstyle=\color{cyan},
  morekeywords={xmlns,version,type}% list your attributes here
}

HTTP

\definecolor{darkblue}{rgb}{0.0,0.0,0.6}

\lstdefinelanguage{http}
{
	keywordstyle=\color{darkblue},
	morekeywords={GET,POST,PUT,DELETE}
}

JSON

\definecolor{darkblue}{rgb}{0.0,0.0,0.6}

\lstdefinelanguage{json}{
	morestring=[s]{:}{,},
	identifierstyle=\color{darkblue},
	stringstyle=\color{black}
}
Include a file with code
\lstinputlisting[language=Java,caption=My Program]{path/to/file/Example.java}
Include code directly
\begin{lstlisting}[language=Java,caption=My Program]
    void insert(int v){
        // we need to look at the right
        if (node < v){
            [...]
        }
        // we need to look at the left
        if (node > v){
            [...]
        }
    }
\end{lstlisting}

Express pseudocode

Further information: Algorithms and Pseudocode

\usepackage{algorithm}
\usepackage{algpseudocode}
\usepackage{algorithmicx}
\begin{algorithm}[H]
	\caption{maxFa}
	\label{alg:maxFa}
	\begin{algorithmic}
		\State Input: Set $A$, sets $U_1,\dots,U_n$
		\State
		\State $U_{max}=\emptyset$ 
		\State $w_{max}=0$
		\ForAll{$U_i$}
			\State Calculate $W=A \cup U_i$
			\If{$|W|>w_{max}$}		
				\State $w_{max}=|W|$ 
				\State $U_{max}=U_i$ 
			\EndIf
		\EndFor

		\Return $U_{max}$
	\end{algorithmic}
\end{algorithm}

See also