% \iffalse meta-comment
%
% Copyright (c) 2007 by Andreas B\"uhmann
% Copyright (c) 2012 by Michael Ummels <michael.ummels@rwth-aachen.de>
%
% This work may be distributed and/or modified under the
% conditions of the LaTeX Project Public License, either version 1.3c
% of this license or (at your option) any later version.
% The latest version of this license is in
%   http://www.latex-project.org/lppl.txt
% and version 1.3 or later is part of all distributions of LaTeX
% version 2005/12/01 or later.
%
% This work has the LPPL maintenance status `maintained'.
%
% The Current Maintainer of this work is Michael Ummels.
%
% This work consists of the files tabfigures.dtx, tabfigures.ins and
% the derived files tabfigures.sty and tabfigures.pdf.
%
% \fi
%
%\iffalse
%<*driver>
\ProvidesFile{tabfigures.dtx}[2012/01/24 v1.1 Patches for using tabular figures]
%</driver>
%<package>\NeedsTeXFormat{LaTeX2e}
%<package>\ProvidesPackage{tabfigures}[2012/01/24 v1.1 Patches for using tabular figures]
%<*driver>
\documentclass[fleqn]{ltxdoc}

\usepackage[T1]{fontenc}
\IfFileExists{MinionPro.sty}{
  \usepackage[smallfamily]{MinionPro}
  \usepackage[scaled=0.8]{berasans}
  \usepackage[scaled=0.8]{beramono}
}{
  \usepackage{lmodern}
}
\usepackage[toc,eqno,enum,bib,lineno,debug]{tabfigures}
\usepackage[fleqn]{mathtools}
\usepackage{hologo}
\usepackage{array}
\usepackage{hypdoc}
\usepackage{microtype}

\hypersetup{
  bookmarksnumbered,
  colorlinks=false,
  pdfborder={0 0 0},
  pdftitle={Using tabular figures with LaTeX},
  pdfauthor={Andreas B\"uhmann, Michael Ummels}
}

% markup
\newcommand*\pkg[1]{\textsf{#1}}
\let\cls\pkg
\let\bst\pkg

\makeatletter
\let\Tf\tabfig@tbf
\makeatother

\newenvironment{sample}{%
  \begin{trivlist}%
  \item
}{%
  \end{trivlist}%
}

\begin{document}
\DocInput{tabfigures.dtx}
\end{document}
%</driver>
%<*doc>
%\fi
%
% \CheckSum{242}
% \CharacterTable
%  {Upper-case    \A\B\C\D\E\F\G\H\I\J\K\L\M\N\O\P\Q\R\S\T\U\V\W\X\Y\Z
%   Lower-case    \a\b\c\d\e\f\g\h\i\j\k\l\m\n\o\p\q\r\s\t\u\v\w\x\y\z
%   Digits        \0\1\2\3\4\5\6\7\8\9
%   Exclamation   \!     Double quote  \"     Hash (number) \#
%   Dollar        \$     Percent       \%     Ampersand     \&
%   Acute accent  \'     Left paren    \(     Right paren   \)
%   Asterisk      \*     Plus          \+     Comma         \,
%   Minus         \-     Point         \.     Solidus       \/
%   Colon         \:     Semicolon     \;     Less than     \<
%   Equals        \=     Greater than  \>     Question mark \?
%   Commercial at \@     Left bracket  \[     Backslash     \\
%   Right bracket \]     Circumflex    \^     Underscore    \_
%   Grave accent  \`     Left brace    \{     Vertical bar  \|
%   Right brace   \}     Tilde         \~}
%
% \GetFileInfo{tabfigures.dtx}
%
% \title{Using tabular figures with \LaTeX}
% \author{Andreas B\"uhmann \and Michael Ummels}
% \date{\fileversion\ -- \filedate}
% \maketitle
%
% \begin{abstract}
%  The \pkg{tabfigures} package is a collection of patches for using
%  tabular figures in some LaTeX environments where numbers should
%  line up vertically such as the table of contents and enumerations.
% \end{abstract}
%  
%
% \section{Introduction}
% 
% Traditionally, there has been no distinction between proportional
% figures (\proportionalfigures{0123456789}) and tabular figures%
% \marginpar{%
%   \raggedleft\LARGE
%   \begin{tabular}[t]{r>{\Tf}r}
%     51 & 51 \\
%     60 & 60 \\
%   \end{tabular}
% } ({\Tf 0123456789}) in \LaTeX. Effectively, the fonts commonly in use
% with \LaTeX\ only include figures of uniform width. This is why, with fonts
% like Minion Pro, which use proportional figures by default, you
% have to specifically adapt your document class or even the \LaTeX\
% kernel to employ tabular figures in the right places:
% the table of contents, enumerations and other material where
% numbers should line up vertically. The \pkg{tabfigures} package tries
% to assist you in the most common situations.
% Throughout this document, tabular figures are colored
% \textcolor{green!50!black}{green}.
%
% \section{Usage}
%
% The current version of this package requires \eTeX\ and the
% \pkg{etoolbox} package (version 2.0a or later). You can load this
% package by putting
% \begin{quote}
% |\usepackage|\oarg{options}|{tabfigures}|
% \end{quote}
% in the preamble of your document. The options, which are described
% in the next sections, control in which parts of your document
% tabular figures are used. (If no options are specified, the package has
% no effect.)
%
% To have any effect, this package relies on the existence of a command
% |\tbfigures| to switch to tabular figures. For instance, the
% \pkg{MinionPro} package defines this command. (More precisely,
% the \pkg{fontaxes} package, which is included by \pkg{MinionPro},
% defines this command). However, this package can also be used as a
% companion to the \pkg{fontspec} package: if \pkg{fontspec} is
% loaded (and |\tbfigures| is not defined), this package defines
% |\tbfigures| as an alias for |\addfontfeatures{Numbers=Monospaced}|.
% In this way, \pkg{tabfigures} can also be used with \hologo{XeTeX} and
% \hologo{LuaTeX} (provided a suitable OpenType font is loaded with
% \pkg{fontspec}).
%
% \section{Options}
%
% \DescribeMacro{toc}
% The \hyperdef{sample}{toc}{|toc|} option controls whether
% tabular figures are used for the numbering and page numbers in the
% table of contents, the list of tables/figures, and similar lists.
%   \begin{sample}
%     \contentsline{section}{%
%       \numberline{4}Moving Information Around}{65}{sample.toc} %
%     \contentsline{subsection}{%
%       \numberline{4.1}The Table of Contents}{66}{sample.toc} %
%     \contentsline{subsection}{%
%       \numberline{4.2}Cross-References}{67}{sample.toc} %
%   \end{sample}
%
% \noindent\DescribeMacro{eqno}
% The |eqno| option enables tabular figures in equation numbers.
%   \begin{sample}
%     \setlength{\abovedisplayskip}{0pt}%
%     \setlength{\belowdisplayskip}{0pt}%
%     \setcounter{equation}{18}%
%     \begin{align}
%       x&=y       & X&=Y       & a&=b+c\\
%       x'&=y'     & X'&=Y'     & a'&=b\\
%       x+x'&=y+y' & X+X'&=Y+Y' & a'b&=c'b
%     \end{align}
%   \end{sample}
%
% \noindent\DescribeMacro{enum}
% The |enum| option turns on tabular figures in enumerations.
%   \begin{sample}
%     \begin{enumerate} \setcounter{enumi}{38}
%     \item The world's fastest supercomputer will have its speed
%       measured in ``petaflops'', which represent 1,000 trillion
%       calculations per second.
%     \item The medical name for the part of the brain associated with
%       teenage sulking is ``superior temporal sulcus''.
%     \item Some Royal Mail stamps, which of course carry the Queen's
%       image, are printed in Holland.
%     \end{enumerate}
%   \end{sample}
%
% \noindent\DescribeMacro{bib}
% The |bib| option activates tabular figures for the labels in the bibliography.
%   \begin{sample}
%     \renewcommand\section[2]{}%
%     \begin{thebibliography}{99}
%     \bibitem[19]{b19} Leslie Lamport. \LaTeX: A Document Preparation
%       System. Addison-Wesley, Reading, MA, 2nd Edition, 1994.
%     \bibitem[20]{b20} American Mathematical Society. User's Guide
%       for the \pkg{amsmath} Package (Version 2.0). Revised, 2002.
%     \bibitem[21]{b21} BBC News.
%       \href{http://www.bbc.co.uk/blogs/magazinemonitor/2006/12/100_things_we_didnt_know_last_2.shtml}%
%       {100 Things We Didn't Know Last Year.} 28 December 2006.
%     \end{thebibliography}
%   \end{sample}
% (The previous examples have been taken from these sources.)
% \medskip
%
% \noindent\DescribeMacro{lineno}
% The |lineno| option enables tabular figures for line numbers
% in conjunction with the \pkg{doc}, \pkg{hypdoc}, \pkg{listings} or
% \pkg{fancyvrb} package.
% \setcounter{CodelineNo}{48}%
%    \begin{macrocode}
\DeclareOption{eqno}{%
  \AtBeginDocument{%
    \@ifpackageloaded{amsmath}{%
%    \end{macrocode}
%
% \section{Compatibility}
%
% This package has been designed to work with the default settings of
% the standard document classes \cls{article}, \cls{report} and
% \cls{book}, their \pkg{KOMA-Script} counterparts, the \cls{memoir}
% class, the \pkg{amsmath} package, and the standard bibliographic
% styles \bst{numeric} and \bst{alphabetic} of \pkg{biblatex} v0.6a.
%
% \StopEventually
%
% \iffalse
%</doc>
% \fi
%
% \section{Implementation}
%
% \global\setcounter{CodelineNo}{0}
% Ease patching of commands. We store the original meaning of |\cmd|
% in a safe place and access it later (in the redefinition) with
% |\tabfig@\cmd|.
%    \begin{macrocode}
%<*package>
\RequirePackage{etoolbox}
\newcommand*\tabfig@warning[1]{%
  \PackageWarningNoLine{tabfigures}{Command \string#1 could not be patched}}
%    \end{macrocode}
% Debugging.
%    \begin{macrocode}
\newif\iftabfig@debug\tabfig@debugfalse
\DeclareOption{debug}{\tabfig@debugtrue}
%    \end{macrocode}
%
% \subsection{Equation numbers}
% We distinguish between the two most frequent cases: \pkg{amsmath}
% and standard \LaTeX. All of the following patches work by injecting
% |\tabfig@tbf| in the right place. They try to do this as robustly as
% possible by reusing the original definition.
%    \begin{macrocode}
\DeclareOption{eqno}{%
  \AtBeginDocument{%
    \@ifpackageloaded{amsmath}{%
      \let\tabfig@maketag@@@\maketag@@@
      \def\maketag@@@#1{\tabfig@maketag@@@{\tabfig@ttbf{#1}}}%
    }{%
      \expandafter\def\expandafter\tabfig@eqnnum\@eqnnum
      \patchcmd{\tabfig@eqnnum}%
        {\theequation}{\tabfig@ttbf{\theequation}}%
        {\def\@eqnnum{{\tabfig@eqnnum}}}%
        {\tabfig@warning{\@eqnnum}}%
    }%
  }%
}
%    \end{macrocode}
% 
% \subsection{Table of contents}
% And similar lists such as list of figures and list of tables.
%    \begin{macrocode}
\DeclareOption{toc}{%
  \AtBeginDocument{%
%    \end{macrocode}
% Generic. First two command that are used in formatting the lists by
% default.
%    \begin{macrocode}
    \let\tabfig@@dottedtocline\@dottedtocline
    \def\@dottedtocline#1#2#3#4#5{%
      \tabfig@@dottedtocline{#1}{#2}{#3}{#4}{\tabfig@ttbf{#5}}%
    }%
    \let\tabfig@numberline\numberline
    \def\numberline#1{\tabfig@numberline{\tabfig@ttbf{#1}}}%
%    \end{macrocode}
% Then a bunch of |\l@|\meta{level} commands for usually available
% entry types, which might not use the commands from above.
%    \begin{macrocode}
    \let\tabfig@l@part\l@part
    \def\l@part#1#2{\tabfig@l@part{#1}{\tabfig@ttbf{#2}}}
    \let\tabfig@l@chapter\l@chapter
    \def\l@chapter#1#2{\tabfig@l@chapter{#1}{\tabfig@ttbf{#2}}}
    \let\tabfig@l@section\l@section
    \def\l@section#1#2{\tabfig@l@section{#1}{\tabfig@ttbf{#2}}}
    \let\tabfig@l@subsection\l@subsection
    \def\l@subsection#1#2{\tabfig@l@subsection{#1}{\tabfig@ttbf{#2}}}
    \let\tabfig@l@subsubsection\l@subsubsection
    \def\l@subsubsection#1#2{\tabfig@l@subsubsection{#1}{\tabfig@ttbf{#2}}}
    \let\tabfig@l@paragraph\l@paragraph
    \def\l@paragraph#1#2{\tabfig@l@paragraph{#1}{\tabfig@ttbf{#2}}}
    \let\tabfig@l@subparagraph\l@subparagraph
    \def\l@subparagraph#1#2{\tabfig@l@subparagraph{#1}{\tabfig@ttbf{#2}}}
    \let\tabfig@l@figure\l@figure
    \def\l@figure#1#2{\tabfig@l@figure{#1}{\tabfig@ttbf{#2}}}
    \let\tabfig@l@table\l@table
    \def\l@table#1#2{\tabfig@l@table{#1}{\tabfig@ttbf{#2}}}
%    \end{macrocode}
% Special support for parts and chapters in \pkg{memoir}.
%    \begin{macrocode}
    \@ifclassloaded{memoir}{%
      \apptocmd{\cftpartpresnum}%
        {\tabfig@tbf}{}{\tabfig@warning{\cftpartpresnum}}%
      \apptocmd{\cftchapterpresnum}%
        {\tabfig@tbf}{}{\tabfig@warning{\cftchapterpresnum}}%
    }{}%
  }%
}
%    \end{macrocode}
% 
% \subsection{Enumerations}
% Labels in enumerations.
%    \begin{macrocode}
\DeclareOption{enum}{%
  \AtBeginDocument{%
    \patchcmd{\labelenumi}%
      {\theenumi}{\tabfig@ttbf{\theenumi}}%
      {}{\tabfig@warning{\labelenumi}}%
    \patchcmd{\labelenumii}%
      {\theenumii}{\tabfig@ttbf{\theenumii}}%
      {}{\tabfig@warning{\labelenumii}}%
    \patchcmd{\labelenumiii}%
      {\theenumiii}{\tabfig@ttbf{\theenumiii}}%
      {}{\tabfig@warning{\labelenumiii}}%
    \patchcmd{\labelenumiv}%
      {\theenumiv}{\tabfig@ttbf{\theenumiv}}%
      {}{\tabfig@warning{\labelenumiv}}%
  }%
}
%    \end{macrocode}
% 
% \subsection{Bibliography}
% Labels in the bibliography.
%    \begin{macrocode}
\DeclareOption{bib}{%
  \AtBeginDocument{%
    \let\tabfig@@biblabel\@biblabel
    \def\@biblabel#1{\tabfig@@biblabel{\tabfig@ttbf{#1}}}%
    \@ifpackageloaded{biblatex}{%
      \DeclareFieldFormat{labelnumberwidth}{\mkbibbrackets{\tabfig@ttbf{#1}}}%
      \DeclareFieldFormat{labelalphawidth}{\mkbibbrackets{\tabfig@ttbf{#1}}}%
    }{}%
  }%
}
%    \end{macrocode}
%
% \subsection{Line numbers}
%    \begin{macrocode}
\DeclareOption{lineno}{%
  \AtBeginDocument{%
%    \end{macrocode}
% Support for the \pkg{doc} and \pkg{hypdoc} packages.
%    \begin{macrocode}
    \@ifpackageloaded{hypdoc}{%
      \ifpatchable{\theCodelineNo}{\HDorg@theCodelineNo}{%
        \patchcmd{\HDorg@theCodelineNo}%
          {\arabic{CodelineNo}}%
          {\tabfig@ttbf{\arabic{CodelineNo}}}%
          {}{\tabfig@warning{\HDorg@theCodelineNo}}%
      }{%
        \patchcmd{\theCodelineNo}%
          {\arabic{CodelineNo}}%
          {\tabfig@ttbf{\arabic{CodelineNo}}}%
          {}{\tabfig@warning{\theCodelineNo}}%
      }%
    }{%
      \@ifpackageloaded{doc}{%
        \patchcmd{\theCodelineNo}%
          {\arabic{CodelineNo}}%
          {\tabfig@ttbf{\arabic{CodelineNo}}}%
          {}{\tabfig@warning{\theCodelineNo}}%
      }{}%
    }%
%    \end{macrocode}
% Support for the \pkg{listings} package.
%    \begin{macrocode}
    \@ifpackageloaded{listings}{%
      \apptocmd{\lst@numberstyle}{\tabfig@tbf}%
        {}{\tabfig@warning{\lst@numberstyle}}%
    }{}%
%    \end{macrocode}
% Support for the \pkg{fancyvrb} package.
%    \begin{macrocode}
    \@ifpackageloaded{fancyvrb}{%
      \patchcmd{\theFancyVerbLine}%
        {\arabic{FancyVerbLine}}%
        {\tabfig@ttbf{\arabic{FancyVerbLine}}}%
        {}{\tabfig@warning{\theFancyVerbLine}}%
    }{}
  }%
}
\ProcessOptions\relax
%    \end{macrocode}
%
% \subsection{Auxiliary macros}
% These commands are used for switching to tabular figures. This can be
% redefined to allow debugging, disabling, etc.
%    \begin{macrocode}
\newcommand*\tabfig@tbf{\tbfigures}
\DeclareTextFontCommand{\tabfig@ttbf}{\tabfig@tbf}
%    \end{macrocode}
% Visual debugging: Set tabular figures (produced by this package) in
% green.
%    \begin{macrocode}
\iftabfig@debug
  \RequirePackage{xcolor}%
  \colorlet{tabfig@debug}{green!50!black}%
  \renewcommand\tabfig@tbf{\tbfigures\color{tabfig@debug}}%
\fi
%    \end{macrocode}
% Check if figure versions are supported at all. If not, we cannot do
% anything useful.
%    \begin{macrocode}
\AtBeginDocument{%
  \@ifundefined{tbfigures}{%
    \@ifpackageloaded{fontspec}{%
      \def\tbfigures{\addfontfeatures{Numbers=Monospaced}}%
    }{%
      \PackageWarningNoLine{tabfigures}{There is no command
        \string\tbfigures\space to support tabular figures}%
      \let\tabfig@tbf\@empty
    }%
  }{}%
}
%</package>
%    \end{macrocode}
%
% \Finale
\endinput
