TeX Tricks

Skip contents

Contents

  1. Sections and definitions
  2. Vertically aligned itemize
  3. Multicol
  4. Overbrace over some columns of a table
  5. Add text to the left of each row and on the top of each column of a matrix
  6. Dashed underline
  7. Allow linebreaks after comma in math mode
  8. Stack two indices in math
  9. Calligraphic font
  10. Make \left( and \right) produce smaller brackets
  11. Footnotes in tabular
  12. Centering tables which are too wide
  13. Split algorithm on multiple pages
  14. Let " produce opening/closing hyphen
  15. Get only the outer shape filled (MetaPost)
  16. Change the space between tabular/array rows and columns
  17. Test the value of an argument of a command
  18. Put short text in the upper right corner of the first page
  19. Indent the text, but not the first line
  20. Change the left-margin of a list (enumerate/itemize)
  21. How to obtain diagonal dots positioned according to the opposite diagonal
  22. How to obtain a centered and a boxed verbatim
  23. How to obtain a table with dashed lines, shaded cells and arrows pointing at a row and a column
  24. How to obtain correct spacing around square brackets (used for intervals) in math mode
  25. How to input UTF-8 characters directly in LaTeX
  26. How to draw a line in TikZ between two points extending at both sides

1. Sections and definitions

\documentclass[10pt]{article}

\newcounter{mydefc}[section]
\setcounter{mydefc}{0}
%\makeatletter
%\renewcommand{\themydefc}{\thesection.\number\c@mydefc}
%\makeatother
\renewcommand{\themydefc}{\thesection.\number\value{mydefc}}
\newenvironment{dfn}{\refstepcounter{mydefc}%
  {\bf Definition \themydefc} \bgroup\it}%
  {\egroup\par\bigskip}

\begin{document}

\section{Test}
\section{Test}
\section{Test}

\label{testsec}

\begin{dfn} \label{testdfn}
Test
\end{dfn}

Section \ref{testsec}
Definition \ref{testdfn}

\end{document}

[ Top ]

2. Vertically aligned itemize

\documentclass[10pt]{article}
\def\I{\mathcal{I}}
\def\T{\mathcal{T}}
\begin{document}

\hbox{%
  \hbox to .52\linewidth{\vbox{\begin{itemize}
    \itemsep -.1em
    \item (P.1) and (P.2)
    \item the residuation principle
  \end{itemize}}\hss}
  \hbox to .44\linewidth{\vbox{\begin{itemize}
    \itemsep -.1em
    \item $\I_\T(D,D) \subseteq D$
    \item $\T((0,0),(0,0)) = 0_{L^*}$
    \item $\T((0,0),(0,0)) = 0_{L^*}$
  \end{itemize}}\hss}}

\hbox{%
  \hbox to .52\linewidth{\vtop{\begin{itemize}
    \itemsep -.1em
    \item (P.1) and (P.2)
    \item the residuation principle
  \end{itemize}}\hss}
  \hbox to .44\linewidth{\vtop{\begin{itemize}
    \itemsep -.1em
    \item $\I_\T(D,D) \subseteq D$
    \item $\T((0,0),(0,0)) = 0_{L^*}$
    \item $\T((0,0),(0,0)) = 0_{L^*}$
  \end{itemize}}\hss}}

\end{document}

[ Top ]

3. Multicol

\documentclass[10pt]{article}
\usepackage{multicol}
\begin{document}

\noindent
Test Test Test Test Test Test Test Test
Test Test Test Test Test Test Test Test
Test Test Test Test Test Test Test Test
Test Test Test Test Test Test Test Test

\begin{multicols}{4}
\begin{itemize}
\item test
\item test
\item test
\item test
\item test
\item test
\item test
\item test
\item test
\item test
\item test
\item test
\item test
\item test
\end{itemize}
\end{multicols}

\noindent
Test Test Test Test Test Test Test Test
Test Test Test Test Test Test Test Test
Test Test Test Test Test Test Test Test
Test Test Test Test Test Test Test Test

\setlength{\columnseprule}{.4pt}
\begin{multicols}{3}
\begin{enumerate}
\item Test
\item Test
\item Test
\item Test
\item Test
\item Test
\item Test
\item Test
\item Test
\item Test
\item Test
\item Test
\item Test
\item Test
\item Test
\item Test
\item Test
\item Test
\item Test
\item Test
\item Test
\item Test
\item Test
\item Test
\item Test
\item Test
\item Test
\item Test
\item Test
\item Test
\item Test
\end{enumerate}
\end{multicols}

\noindent
Test Test Test Test Test Test Test Test
Test Test Test Test Test Test Test Test
Test Test Test Test Test Test Test Test
Test Test Test Test Test Test Test Test

\end{document}

[ Top ]

4. Overbrace over some columns of a table

\documentclass[10pt]{article}
\usepackage{multirow}
\begin{document}

\[
  \left(
  \begin{array}{r|ccc}
    a & b & \ldots & c\\
    \hline
    d\\
    \vdots & & A\\
    e
  \end{array}
  \right)
\]

\makeatletter
\def\Biggg#1{{\hbox{$\left#1\vbox to32\p@{}\right.\n@space$}}}
\newdimen\bracketwidth
\settowidth{\bracketwidth}{\Biggg(}
\makeatother
\[
%  \begin{array}{r@{}r@{\hspace{\arraycolsep}}rcccc@{}l}
  \begin{array}{r@{}r@{\hspace{\arraycolsep}}rcc%
    c@{\hspace{\arraycolsep}}c@{}l}
    & & & & \multicolumn{3}{c}{\vspace{-.5em}
      \overbrace{\hphantom{b \hspace{2\arraycolsep} \ldots
      \hspace{2\arraycolsep} c}}^{n \mbox{\scriptsize\ times}}}\\
    \multirow{5}{\bracketwidth}[3pt]{\Biggg(}
    & & a & \multirow{5}{1pt}[3pt]{\vrule height 52pt} & b & \ldots & c &
    \multirow{5}{\bracketwidth}[3pt]{\Biggg)}\\
    \cline{2-7}
    & & d\\
    & & \vdots & & & A\\
    & & e
  \end{array}
\]
\end{document}

\documentclass{article}

%\makeatletter
%\def\overbrace#1{\mathop{\vbox{\m@th\ialign{##\crcr\noalign{\kern3\p@}%
%      \downbracefill\crcr\noalign{\kern3\p@\nointerlineskip}%
%      $\hfil\displaystyle{#1}\hfil$\crcr}}}\limits}
%\makeatother

\begin{document}

\[
  \vbox{%
  \hskip2.8em$\overbrace{\hphantom{b \hspace{2\arraycolsep} \ldots
    \hspace{2\arraycolsep} c}}^{n \mbox{\scriptsize\ times}}$
  \vskip-.25em
  $\left(
  \begin{array}{r|ccc}
    a & b & \ldots & c\\
    \hline
    d\\
    \vdots & & A\\
    e
  \end{array}
  \right)$%
  }
\]

\end{document}

\documentclass{article}

\def\moverbrace#1#2{%
  \newdimen\moverbracewd%
  \settowidth\moverbracewd{#1}%
  \addtolength\moverbracewd{-2\arraycolsep}
  \vbox to 1.6ex{\hsize=\moverbracewd\centering\vss
  $\overbrace{#1}^{#2}$}%
}

\begin{document}

\[
  \left(
  \begin{array}{r|c}
    a & \moverbrace{b \hspace{2\arraycolsep} \ldots
      \hspace{2\arraycolsep} c}{n \mbox{\scriptsize\ times}}\\
    \hline
    d\\
    \vdots & A\\
    e
  \end{array}
  \right)
\]

\end{document}

\documentclass{article}

\def\moverbrace#1#2{%
  \newdimen\moverbracewd%
  \settowidth\moverbracewd{#1}%
  \vbox to 1.6ex{\hsize=\moverbracewd\centering\vss
  $\overbrace{#1}^{#2}$}%
}

\begin{document}

\[
  \left(
  \begin{array}{c|c}
    a & \moverbrace{b \hskip2\arraycolsep \ldots
      \hskip2\arraycolsep c}{n \mbox{\scriptsize\ times}}\\
    \hline
    d\\
    \vdots & A\\
    e
  \end{array}
  \right)
\]

\end{document}

The following can be more widely used than the above.

\documentclass{article}

\makeatletter
\newlength{\taboverbracewd}
\newlength{\taboverbracesep}
\newlength{\tabunderbracewd}
\newlength{\tabunderbracesep}
\def\taboverbrace{\@ifnextchar[{\tabover@brace}{\tabover@brace[]}}
\def\tabover@brace[#1]#2#3{%
  \setlength{\taboverbracewd}{#2}%
  \setlength{\taboverbracesep}{1em}%
  \def\taboverbraceaddsep{#1}%
  \ifx\taboverbraceaddsep\@empty\else%
    \addtolength{\taboverbracesep}{\taboverbraceaddsep}%
  \fi%
  \smash{%
    \raise\taboverbracesep\vbox to 1.6ex{\hsize=\taboverbracewd%
      \centering\vss%
      $\overbrace{\hspace{#2}}^{#3}$%
    }%
    \hspace{-#2}%
  }%
}
\def\tabunderbrace{\@ifnextchar[{\tabunder@brace}{\tabunder@brace[]}}
\def\tabunder@brace[#1]#2#3{%
  \setlength{\tabunderbracewd}{#2}%
  \setlength{\tabunderbracesep}{.5em}%
  \def\tabunderbraceaddsep{#1}%
  \ifx\tabunderbraceaddsep\@empty\else%
    \addtolength{\tabunderbracesep}{\tabunderbraceaddsep}%
  \fi%
  \tabunderbracesep=-\tabunderbracesep%
  \smash{%
    \raise\tabunderbracesep\vbox to 1.6ex{\hsize=\tabunderbracewd%
      \centering\vss%
      $\underbrace{\hspace{#2}}_{#3}$%
    }%
    \hspace{-#2}%
  }%
}
%\def\taboverbrace#1#2{%
%  \newlength{\taboverbracewd}%
%  \setlength{\taboverbracewd}{#1}%
%  \smash{\raise1em\vbox to 1.6ex{\hsize=\taboverbracewd\centering\vss
%  $\overbrace{\hspace{#1}}^{#2}$}\hspace{-#1}}%
%}
%\def\tabunderbrace#1#2{%
%  \newlength{\tabunderbracewd}%
%  \setlength{\tabunderbracewd}{#1}%
%  \smash{\raise-.5em\vbox to 1.6ex{\hsize=\tabunderbracewd\centering\vss
%  $\underbrace{\hspace{#1}}_{#2}$}\hspace{-#1}}%
%}
\makeatother

\begin{document}

\[
  \begin{array}{r|llll}
    f(x) & x^2 & \exp(x) + \sin(x) & \frac{1}{x} & x^3 \\
    \hline
    f'(x) & 2x & \exp(x) + \cos(x) & -\frac{1}{x^2} & 3x^2
  \end{array}
\]

\[
  \begin{array}{r|llll}
    f(x) & x^2 & \exp(x) + \sin(x) & \frac{1}{x} & x^3 \\
    \hline
    f'(x) & 2x & \tabunderbrace{10em}{\mbox{no power functions}} \exp(x) + \cos(x) & -\frac{1}{x^2} & 3x^2
  \end{array}
\]

\vspace{3em}

\[
  \begin{array}{r|llll}
    f(x) & x^2 & \taboverbrace{10em}{\mbox{no power functions}} \exp(x) + \sin(x) & \frac{1}{x} & x^3 \\
    \hline
    f'(x) & 2x & \exp(x) + \cos(x) & -\frac{1}{x^2} & 3x^2
  \end{array}
\]

\vspace{1em}

\[
  \begin{array}{r|llll}
    f(x) & x^2 & \taboverbrace[.5em]{10em}{\mbox{no power functions}} \exp(x) + \sin(x) & \displaystyle\frac{1}{x} & x^3 \\
    \hline
    f'(x) & 2x & \exp(x) + \cos(x) & \displaystyle-\frac{1}{x^2} & 3x^2
  \end{array}
\]

\end{document}

[ Top ]

5. Add text to the left of each row and on the top of each column of a matrix

\documentclass{article}
\begin{document}
\begin{math}
\bordermatrix{& a_1 & a_2 & ... & a_n  \cr
          b_1 & 1.2 & 3.3 & 5.1 & 2.8  \cr
          c_1 & 4.7 & 7.8 & 2.4 & 1.9  \cr
          ... & ... & ... & ... & ...  \cr
          z_1 & 8.0 & 9.9 & 0.9 & 9.99 \cr}
\end{math}
\end{document}

[ Top ]

6. Dashed underline

\documentclass[10pt]{article}
\usepackage{ulem}

\def\dotuline{\bgroup 
  \ifdim\ULdepth=\maxdimen  % Set depth based on font, if not set already
   \settodepth\ULdepth{(j}\advance\ULdepth.4pt\fi
  \markoverwith{\begingroup
  \advance\ULdepth0.08ex
  \lower\ULdepth\hbox{\kern.15em .\kern.1em}%
  \endgroup}\ULon}

\def\dashuline{\bgroup 
  \ifdim\ULdepth=\maxdimen  % Set depth based on font, if not set already
   \settodepth\ULdepth{(j}\advance\ULdepth.4pt\fi
  \markoverwith{\kern.15em
  \vtop{\kern\ULdepth \hrule width .3em}%
  \kern.15em}\ULon}

\begin{document}

\dotuline{Test test test}\\
\dashuline{Test test test}\\
\dashuline{Test test test test test test test test test test test test 
  test test test test test test test test test test}

\end{document}

[ Top ]

7. Allow linebreaks after comma in math mode

%%% Split globally

\documentclass{report}

\usepackage{mathptmx}

\makeatletter
\def\old@comma{,}
\catcode`\,=13
\def,{%
  \ifmmode%
    \old@comma\discretionary{}{}{}%
%    \old@comma$ $%
%    $\old@comma\ $%
  \else%
    \old@comma%
  \fi%
}
\makeatother

\begin{document}

$\frac{x+1}{2}, 5x^2+3x-4, \sqrt{6x-9}, \frac{x-7}{8},
\frac{x+1}{2}, 5x^2+3x-4, \sqrt{6x-9}, \frac{x-7}{8},
\frac{x+1}{2}, 5x^2+3x-4, \sqrt{6x-9}, \frac{x-7}{8},
\frac{x+1}{2}, 5x^2+3x-4, \sqrt{6x-9}, \frac{x-7}{8},
\frac{x+1}{2}, 5x^2+3x-4, \sqrt{6x-9}, \frac{x-7}{8}$

test, test, test, test, test, test, test, test, test,
test, test, test, test, test, test, test, test, test,
test, test, test, test, test, test, test, test, test,
test, test, test, test, test, test, test, test, test

\end{document}

%%% Split locally

\documentclass{report}

\usepackage{mathptmx}

\begin{document}

$\frac{x+1}{2}, 5x^2+3x-4, \sqrt{6x-9}, \frac{x-7}{8},
\frac{x+1}{2}, 5x^2+3x-4, \sqrt{6x-9}, \frac{x-7}{8},
\frac{x+1}{2}, 5x^2+3x-4,\discretionary{}{}{} \sqrt{6x-9}, \frac{x-7}{8},
\frac{x+1}{2}, 5x^2+3x-4, \sqrt{6x-9}, \frac{x-7}{8},
\frac{x+1}{2}, 5x^2+3x-4, \sqrt{6x-9}, \frac{x-7}{8}$

\end{document}

[ Top ]

8. Stack two indices in math

\documentclass{article}

\makeatletter
\def\stack#1#2{%
  \mathchoice
    {\@stack{\displaystyle}{#1}{#2}}%
    {\@stack{\textstyle}{#1}{#2}}%
    {\@stack{\scriptstyle}{#1}{#2}}%
    {\@stack{\scriptscriptstyle}{#1}{#2}}%
}
\newdimen\@stackwda
\newdimen\@stackwdb
\def\@stack#1#2#3{%
  \settowidth{\@stackwda}{$#1 #2$}%
  \settowidth{\@stackwdb}{$#1 #3$}%
  \advance\@stackwda-\@stackwdb
  \@stackwda=.5\@stackwda
  \everymath{#1\everymath{}}%
  \renewcommand{\arraystretch}{.6}%
  \setlength{\arraycolsep}{0em}%
  \ifnum\@stackwda<\z@ \hskip\@stackwda\fi%
  \begin{array}[t]{c}
    #2\\#3
  \end{array}
  \ifnum\@stackwda<\z@ \hskip\@stackwda\fi%
}
\makeatother

\begin{document}
$\displaystyle \lim_{x_2 \stack{\to}{>} x_{0,2}} 
\mathcal{T}([x_{0,1}, x_2], [x_{0,1}, x_2]) = x_0$
\[
  \sum_{\stack{i,j \in \{1,2,\ldots,n\}}{i < j}} f_{ij}
\]
\end{document}

\documentclass[a4paper,11pt]{article}
\usepackage{amsmath}
\newcommand{\stack}[2]{\genfrac{}{}{0pt}{}{#1}{#2}}
\begin{document}
$\displaystyle \lim_{x_2 \stack{\to}{>} x_{0,2}} 
\mathcal{T}([x_{0,1}, x_2], [x_{0,1}, x_2]) = x_0$
\[
  \sum_{\stack{i,j \in \{1,2,\ldots,n\}}{i < j}} f_{ij}
\]
\end{document}

\documentclass{article}
\usepackage{amsmath}
\begin{document}
$\displaystyle \lim_{\substack{x_2 \to x_{0,2}\\%
  \hphantom{x_2\ }>{\hphantom{\ x_{0,2}}}}} 
  \mathcal{T}([x_{0,1}, x_2], [x_{0,1}, x_2]) = x_0$
\[
  \sum_{\substack{i,j \in \{1,2,\ldots,n\}\\i < j}} f_{ij}
\]
\end{document}

\documentclass{article}

\makeatletter
\def\buildstack#1\under#2{\mathrel{\mathop{\kern\z@#2}\limits_{#1}}}
\makeatother

\makeatletter
\def\stack#1#2{\mathrel{\mathop{\kern\z@#1}\limits_{#2}}}
\makeatother

\begin{document}

$\lim\limits_{x \buildstack > \under \to 0} f(x)$
$\lim\limits_{x \stack{\to}{>} 0} f(x)$

\end{document}

[ Top ]

9. Calligraphic font

\documentclass{article}
\begin{document}
\usefont{OMS}{ztmcm}{m}{n}\selectfont ABCDEFGHIJKLMNOPQRSTUVWXYZ
\end{document}

\documentclass{article}
\usepackage{eucal}
\begin{document}
$\mathcal{ABCDEFGHIJKLMNOPQRSTUVWXYZ}$
\end{document}

[ Top ]

10. Make \left( and \right) produce smaller brackets

\documentclass{article}
\usepackage{amsbsy}
\newcommand{\E}[1]{%
  \setbox8\hbox{$#1$}%
  \newdimen\tempheight
  \tempheight=\ht8
  \advance\tempheight-.2em
  \:\boldsymbol{E}\!
    \left(\vbox to \tempheight{}\right.
    \kern-.25em #1\kern-.25em
    \left.\vbox to \tempheight{}\right)
}
\begin{document}
$\E{X}$\\
$\:\boldsymbol{E}\!\left(X\right)$\\
$\E{\frac{a}{b}}$\\
$\:\boldsymbol{E}\!\left(\frac{a}{b}\right)$\\
$\E{\displaystyle\int f(x) dx}$\\
$\:\boldsymbol{E}\!\left(\displaystyle\int f(x) dx\right)$
\end{document}

[ Top ]

11. Footnotes in tabular

\documentclass{article}
\usepackage{footnote}
\makesavenoteenv{tabular}
\begin{document}

\begin{tabular}{|c|c|}
  \hline
  donn\'ee1\footnotemark[1] & donn\'ee2\footnotemark[2] \\
  \hline
\end{tabular}
\footnotetext[1]{Note associ\'ee \`a la donn\'ee 1.}
\footnotetext[2]{Note associ\'ee \`a la donn\'ee 2.}

Test\footnote{Test}.

\vspace{1cm}
%\begin{savenotes} % Use this when you don't have
                   % \makesavenoteenv{tabular} in the preamble
\begin{tabular}{|c|c|}
  \hline
  donn\'ee3\footnote{Note associ\'ee \`a la donn\'ee 3.} &
  donn\'ee4\footnote{Note associ\'ee \`a la donn\'ee 4.} \\
  \hline
\end{tabular}
%\end{savenotes}

\end{document}

[ Top ]

12. Centering tables which are too wide

\documentclass{article}

\begin{document}

\begin{table}
\hbox to \textwidth{
\hss
%\begin{tabular*}{1.5\textwidth}{@{\extracolsep{\fill}}%
\begin{tabular}{%
ccc|ccc|ccc|%
ccc|ccc|ccc|%
ccc|ccc|ccc%
%@{\extracolsep{\fill}}}
}
\multicolumn{3}{c|}{A} & \multicolumn{3}{c|}{B} & \multicolumn{3}{c|}{C} &
\multicolumn{3}{c|}{A} & \multicolumn{3}{c|}{B} & \multicolumn{3}{c|}{C} &
\multicolumn{3}{c|}{A} & \multicolumn{3}{c|}{B} & \multicolumn{3}{c}{C}\\
a & b & c & a & b & c & a & b & c &
a & b & c & a & b & c & a & b & c &
a & b & c & a & b & c & a & b & c
%\end{tabular*}
\end{tabular}
\hss
}
\end{table}
Test Test Test Test Test Test
Test Test Test Test Test Test
Test Test Test Test Test Test
Test Test Test Test Test Test
Test Test Test Test Test Test
Test Test Test Test Test Test
Test Test Test Test Test Test
\begin{table}
\hbox to \textwidth{
\hss
\begin{tabular}{ccc|ccc|ccc}
\multicolumn{3}{c|}{A} & \multicolumn{3}{c|}{B} & \multicolumn{3}{c}{C}\\
a & b & c & a & b & c & a & b & c
\end{tabular}
\hss
}
\end{table}

\end{document}

[ Top ]

13. Split algorithm on multiple pages

\documentclass{article}

%\usepackage{algorithm}
\usepackage{algorithmic}

\makeatletter
\newcounter{algorithmbis}
\setcounter{algorithmbis}{0}
\renewcommand{\thealgorithmbis}{\thesection.\arabic{algorithmbis}}
\def\algorithmbis{\@ifnextchar[{\@algorithmbisa}{\@algorithmbisb}}
\def\@algorithmbisa[#1]{%
  \refstepcounter{algorithmbis}
  \trivlist
  \leftmargin\z@
  \itemindent\z@
  \labelsep\z@
  \item[\parbox{\textwidth}{%
    \hrule
    \hrule
    \noindent\strut\textbf{Algorithm \thealgorithmbis} #1
    \hrule
  }]\hfil\vskip0em%
}
\def\@algorithmbisb{\@algorithmbisa[]}
\def\endalgorithmbis{\hfil\vskip-1em\hrule\endtrivlist}
\makeatother

\begin{document}

\section{Test}

%\begin{algorithm}
%\caption{Test}\label{alg1}
%\begin{algorithmic}
%\IF{$x \le 0$}
%\STATE $a = b$
%\ENDIF
%\end{algorithmic}
%\end{algorithm}

Test test

\begin{algorithmbis}[Test]\label{alg2}
\begin{algorithmic}
\IF{$x \le 0$}
\STATE $a = b$
\ENDIF
\IF{$x \le 0$}
\STATE $a = b$
\ENDIF
\IF{$x \le 0$}
\STATE $a = b$
\ENDIF
\IF{$x \le 0$}
\STATE $a = b$
\ENDIF
\IF{$x \le 0$}
\STATE $a = b$
\ENDIF
\IF{$x \le 0$}
\STATE $a = b$
\ENDIF
\IF{$x \le 0$}
\STATE $a = b$
\ENDIF
\IF{$x \le 0$}
\STATE $a = b$
\ENDIF
\IF{$x \le 0$}
\STATE $a = b$
\ENDIF
\IF{$x \le 0$}
\STATE $a = b$
\ENDIF
\IF{$x \le 0$}
\STATE $a = b$
\ENDIF
\IF{$x \le 0$}
\STATE $a = b$
\ENDIF
\IF{$x \le 0$}
\STATE $a = b$
\ENDIF
\IF{$x \le 0$}
\STATE $a = b$
\ENDIF
\IF{$x \le 0$}
\STATE $a = b$
\ENDIF
\IF{$x \le 0$}
\STATE $a = b$
\ENDIF
\IF{$x \le 0$}
\STATE $a = b$
\ENDIF
\IF{$x \le 0$}
\STATE $a = b$
\ENDIF
\IF{$x \le 0$}
\STATE $a = b$
\ENDIF
\IF{$x \le 0$}
\STATE $a = b$
\ENDIF
\end{algorithmic}
\end{algorithmbis}

\begin{algorithmbis}\label{alg3}
\begin{algorithmic}
\IF{$x \le 0$}
\STATE $a = b$
\ENDIF
\end{algorithmic}
\end{algorithmbis}

\addtocounter{algorithmbis}{-1}
\begin{algorithmbis}[Test3]\label{alg4}
\begin{algorithmic}
\IF{$x \le 0$}
\STATE $a = b$
\ENDIF
\end{algorithmic}
\end{algorithmbis}

Test
%\ref{alg1},
\ref{alg2},
\ref{alg3},
\ref{alg4}.

\end{document}

[ Top ]

14. Let " produce opening/closing hyphen

% Warning: weird things will happen if you use " also for
%          other purposes, such as umlauts!!!
\documentclass{article}
\makeatletter
\catcode`\"=13
\let\@hyphen@open\@empty
\def"{%
  \ifx\@hyphen@open\@empty%
    \def\@hyphen@open{a}%
    ``%
  \else
    ''%
    \let\@hyphen@open\@empty%
  \fi%
}
\makeatother
\begin{document}
Test ``Hallo'' "Hallo".
\end{document}

[ Top ]

15. Get only the outer shape filled (MetaPost)

beginfig(1);
  u:=72bp;
  path p[];
  p[1]:=(u,0)..(0,u)..(-u,0)..(0,-u)..cycle;
  p[2]:=(u/2,0)..(0,u/2)..(-u/2,0)..(0,-u/2)..cycle;
  fill (p[1]--(reverse p[2])--cycle) withcolor (1,0,0);
  draw p[1];
  draw p[2];
endfig;
end;

[ Top ]

16. Change the space between tabular/array rows and columns

\documentclass{article}
\begin{document}
\setlength{\tabcolsep}{0.14em}
\renewcommand{\arraystretch}{1.2}
\begin{tabular}{rcl}
  x & = & a+b\\
  y & = & c+d
\end{tabular}
% Note: prefer to use the align environment from amsmath instead of the following code!
\setlength{\arraycolsep}{0.14em}
\renewcommand{\arraystretch}{1.2}
$\begin{array}{rcl}
  x & = & a+b\\
  y & = & c+d
\end{array}$
\end{document}

[ Top ]

17. Test the value of an argument of a command

\documentclass{article}

\makeatletter
\def\zzz@value{yyy}
\def\zzz#1{%
  \def\zzz@valueb{#1}%
  \ifx\zzz@valueb\zzz@value%
    hallo%
  \else%
    HALLO%
  \fi%
}
\makeatother

\begin{document}

\zzz{yyy}
\zzz{xxx}

\end{document}

[ Top ]

18. Put short text in the upper right corner of the first page

\documentclass{article}

\def\myrightmark{Test}
\makeatletter
\def\ps@myheadings{%
  \def\@oddhead{\hfil\myrightmark}%
}
\makeatother

\begin{document}

\thispagestyle{myheadings}
test\newpage
test\newpage
test\newpage
test\newpage
test\newpage

\end{document}

\documentclass{article}

\usepackage{fancyhdr}

\begin{document}

\thispagestyle{fancy}
\renewcommand{\headrulewidth}{0pt}
\rhead{Test2}

test\newpage
test\newpage
test\newpage
test\newpage
test\newpage

\end{document}

[ Top ]

19. Indent the text, but not the first line

\documentclass{article}
\begin{document}
\noindent Test
\begin{list}{}{\settowidth\labelwidth{}
               \setlength{\leftmargin}{1em}
               \addtolength{\itemindent}{-1em}}
\item dsqf sqdf qsdf qsdf sdqf sdqf dqsf sdqf sqdf dsqf sqdkj
lmk lmkj lmkj mlkj mkj mlkj mlkj mlkj mlk kl mlk ml jlm kj mlk
lk jmkl jmlkkj mlj mlkj lmkj lmkj mlkj mkj mlkj mlkj mlkj mlk
kl mlk ml jlm kj mlk lk jmkl jmlkkj mlj mlkj lmkj
\item dsqf sqdf qsdf qsdf sdqf sdqf dqsf sdqf sqdf dsqf sqdkj
lmk lmkj lmkj mlkj mkj mlkj mlkj mlkj mlk kl mlk ml jlm kj mlk
lk jmkl jmlkkj mlj mlkj lmkj
\end{list}
\noindent Test
\end{document}

[ Top ]

20. Change the left-margin of a list (enumerate/itemize)

\documentclass{article}

\def\I{\mathcal{I}}
\def\N{\mathcal{N}}

\begin{document}

\noindent Test

\hspace*{1em}\parbox{.8\textwidth}{
  \begin{enumerate}
  \renewcommand{\theenumi}{(A.\arabic{enumi})}
  \renewcommand{\labelenumi}{\theenumi}
  \setlength{\itemsep}{.3em}
  \item \label{A1} $\I(.,y)$ is decreasing\\
    $\I(x,.)$ is increasing
  \item \label{A2} $\I(1_{L^*},x) = x$
  \item \label{A3} $\I(x,y) = \I(\N(y), \N(x))$
  \item \label{A4} $\I(x,\I(y,z)) = \I(y, \I(x,z))$
  \item \label{A5} $x \le_{L^*} y \iff \I(x,y) = 1_{L^*}$
  \item \label{A6} $\I$ is continuous
  \end{enumerate}
}

\noindent Test \ref{A2}

\begin{list}{(A.\arabic{enumi})}{%
  \settowidth\labelwidth{(A.2)}%
  \setlength{\topsep}{0pt}%
  \setlength{\leftmargin}{\labelwidth}%
  \addtolength{\leftmargin}{\labelsep}%
  \setlength{\itemsep}{0em}%
  \setlength{\itemindent}{0em}%
  \usecounter{enumi}%
  \renewcommand{\theenumi}{(A.\arabic{enumi})}}
  \item \label{A1b} $(\forall y \in L^*)(\I(.,y)$ is decreasing
    in $L^*)$\\
    $(\forall x \in L^*)(\I(x,.) $ is increasing in $ L^*)$
    \hfill {\em (monotonicity laws)}
  \item \label{A2b} $(\forall x \in L^*)(\I(1_{L^*},x) = x)$
    \hfill {\em (neutrality principle)}
  \item \label{A3b} $(\forall (x,y) \in (L^*)^2)(\I(x,y) =
    \I(\N_{\I}(y), \N_{\I}(x))$
    \hfill {\em (contrapositivity)}
  \item \label{A4b} $(\forall (x,y,z) \in (L^*)^3)(\I(x,\I(y,z)) =
    \I(y,\I(x,z)))$
    \hfill {\em (interchangeability principle)}
  \item \label{A5b} $(\forall (x,y) \in (L^*)^2)(x \le_{L^*} y \iff
    \I(x,y) = 1_{L^*})$
    \hfill {\em (confinement principle)}
  \item \label{A6b} $\I$ is a continuous $(L^*)^2 \to L^*$ mapping
    \hfill {\em (continuity)}
\end{list}

\noindent Test \ref{A6b}

\settowidth{\leftmargini}{(A.5)}
\addtolength{\leftmargini}{\labelsep}
\begin{enumerate}
  \renewcommand{\theenumi}{(A.\arabic{enumi})}
  \renewcommand{\labelenumi}{\theenumi}
  \setlength{\itemsep}{.3em}
  \item \label{A1c} $\I(.,y)$ is decreasing\\
    $\I(x,.)$ is increasing
  \item \label{A2c} $\I(1_{L^*},x) = x$
  \item \label{A3c} $\I(x,y) = \I(\N(y), \N(x))$
  \item \label{A4c} $\I(x,\I(y,z)) = \I(y, \I(x,z))$
  \item \label{A5c} $x \le_{L^*} y \iff \I(x,y) = 1_{L^*}$
  \item \label{A6c} $\I$ is continuous
\end{enumerate}

\noindent Test \ref{A6c}

\end{document}

[ Top ]

21. How to obtain diagonal dots positioned according to the opposite diagonal

\documentclass{article}
\usepackage{mathdots}
\begin{document}
$\vdots, \cdots, \ldots, \ddots, \iddots$
\end{document}

[ Top ]

22. How to obtain a centered and a boxed verbatim

\documentclass{article}

\usepackage{fancyvrb}

\begin{document}

\noindent
Test test
test test
test test
test test
test test
test test
test test
test test
test test
test test
test test
test test

Centered verbatim:

\begin{center}
\begin{tiny}
\begin{BVerbatim}[boxwidth=auto]
Test test
\end{BVerbatim}
\end{tiny}
\end{center}

Centered verbatim in a box:

\begin{center}
\begin{tiny}
\begin{SaveVerbatim}{MyVerb}
Test test
\end{SaveVerbatim}
\fbox{\BUseVerbatim{MyVerb}}
\end{tiny}
\end{center}

Centered verbatim in a box (without using fancyvrb):

\begin{center}
\begin{tiny}
%\begin{minipage}{0.5\textwidth}
\setbox0\vbox{\hsize=0.45\textwidth
\begin{verbatim}
Test test
\end{verbatim}
}
\fbox{\box0}
%\end{minipage}
\end{tiny}
\end{center}

\noindent
Test test
test test
test test
test test
test test
test test
test test
test test
test test
test test
test test

\end{document}

[ Top ]

23. How to obtain a table with dashed lines, shaded cells and arrows pointing at a row and a column

\documentclass{article}

\usepackage{colortbl}
\usepackage{multirow,bigdelim}
\usepackage{arydshln}

\begin{document}

$\begin{array}{c@{}rc:cccl}
    \multicolumn{4}{c}{} & j &\\
    & & & & \downarrow &\\
    & \ldelim({4}{3.5mm}\!\!\! & \cellcolor[gray]{1} & \cellcolor[gray]{.8} & \cellcolor[gray]{.8} & \cellcolor[gray]{.8} & \!\rdelim){4}{3.5mm}\\
    \cdashline{3-6}
    & & \cellcolor[gray]{.8} & & &\\
    i \rightarrow & & \cellcolor[gray]{.8} & & 1 &\\
    & & \cellcolor[gray]{.8} & & &
\end{array}$

\end{document}

[ Top ]

24. How to obtain correct spacing around square brackets (used for intervals) in math mode

\documentclass{article}

\begin{document}

\noindent
$c = ]a,b[ = d$\\ % bad
$c = {]a,b[} = d$\\ % good
$c = \mathopen]a,b\mathclose[ = d$\\ % good
$c = [a,b] = d$\\ % good
$c | d$\\ % usually this is bad
$c \mathrel| d$\\ % good
$c \mid d$\\ % good
$x \in ]-\infty, 0[$\\ % bad
$x \in {]-\infty, 0[}$\\ % bad
$x \in {]{-\infty}, 0[}$ % good

\end{document}

[ Top ]

25. How to input UTF-8 characters directly in LaTeX

\documentclass{article}

\usepackage[T1]{fontenc}
%\usepackage{utf-8}
%\usepackage[utf8]{inputenc}
% Install latex-ucs for the following or use one of the above:
\usepackage[utf8x]{inputenc}
%\usepackage{ucs}

\begin{document}

\noindent UTF-8 text\\
áéíóúàèìòùâêîôûäëïöüçñÁÉÑÇ\\
øłæßðŋ«»¶←↓→đ“”·¬¼½¡£™±°¿\\
Ł®↑ıØÞ°¯ÆЪŊµ©‘’º×÷\\
%€ħ¢ŧ─⅛⅜⅝⅞ΩŦ¥Ħ
End UTF-8 text

\end{document}

[ Top ]

26. How to draw a line in TikZ between two points extending at both sides

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{calc}

\begin{document}

% Draw line between two points extended on both sides over a length specified by the two arguments of add=<additional length left> and <additional length right> (needs \usetikzlibrary{calc})
\tikzset{add/.style args={#1 and #2}{%
  to path={%
    ($(\tikztostart) ! -#1 ! (\tikztotarget)$) -- ($(\tikztotarget) ! -#2 ! (\tikztostart)$)%
    \tikztonodes}%
  }%
}
\begin{tikzpicture}[x=1cm,y=1cm]
\draw (0,0) to[add=.2 and .2] (1,1);
\fill (0,0) circle (2pt);
\fill (1,1) circle (2pt);
\end{tikzpicture}

\end{document}

[ Top ]

Created by Glad Deschrijver.