% \iffalse meta-comment
%
% Copyright (c) 2007 by Andreas B\"uhmann
% Copyright (c) 2011 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}[2011/09/17 v1.0 Patches for using tabular figures]
%</driver>
%<package>\NeedsTeXFormat{LaTeX2e}
%<package>\ProvidesPackage{tabfigures}[2011/09/17 v1.0 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{array}
\usepackage{hyperref}
\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@font
\makeatother

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

\begin{document}
\DocInput{tabfigures.dtx}
\end{document}
%</driver>
%<*doc>
%\fi
%
% \CheckSum{184}
% \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}
%
% This package is a collection of patches that inject
% |\tbfigures| into the involved formatting commands in
% the right places. 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 which patches 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).
%
% \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.
% Only the \pkg{doc} package is supported for now; support for other
% line-numbering packages is planned.
% \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>
\newcommand*\tabfig@@[1]{tabfig@@\expandafter\@cdr\string #1\@nil}
\newcommand*\tabfig@[1]{\csname \tabfig@@{#1}\endcsname}
\newcommand*\tabfig@name{}
\newcommand*\tabfig@save[1]{%
  \edef\tabfig@name{\expandafter\expandafter\expandafter
    \noexpand\tabfig@{#1}}%
  \expandafter\newcommand\tabfig@name{}%
  \expandafter\let\tabfig@name #1%
}
%    \end{macrocode}
% Just a shorthand.
%    \begin{macrocode}
\newcommand*\tabfig@def[1]{%
  \tabfig@save{#1}%
  \def#1%
}
%    \end{macrocode}
% Patch |\l@|\meta{level} commands. These commands always take two
% arguments, the second of which is the page number.
%    \begin{macrocode}
\newcommand*\tabfig@pname{}
\newcommand*\tabfig@patch@l[1]{%
  \@ifundefined{l@#1}{}{%
    \edef\tabfig@pname{\expandafter\noexpand\csname l@#1\endcsname}%
    \expandafter\tabfig@save\tabfig@pname
    \expandafter\edef\tabfig@pname##1##2{%
      \noexpand\tabfig@
      \expandafter\noexpand\tabfig@pname
      {##1}{\noexpand\tabfig@font ##2}%
    }%
  }%
}
%    \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@font| 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}{%
      \tabfig@def\maketag@@@#1{\tabfig@\maketag@@@{\tabfig@font #1}}%
    }{%
      \CheckCommand*\@eqnnum{{\normalfont \normalcolor (\theequation)}}%
      \def\@eqnnum{{\normalfont \tabfig@font \normalcolor (\theequation)}}%
    }%
  }%
}
%    \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 using in formatting the lists by
% default.
%    \begin{macrocode}
    \tabfig@def\@dottedtocline#1#2#3#4#5{%
      \tabfig@\@dottedtocline{#1}{#2}{#3}{#4}{\tabfig@font #5}%
    }%
    \tabfig@def\numberline#1{%
      \tabfig@\numberline{\tabfig@font #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}
    \tabfig@patch@l{part}%
    \tabfig@patch@l{chapter}%
    \tabfig@patch@l{section}%
    \tabfig@patch@l{subsection}%
    \tabfig@patch@l{subsubsection}%
    \tabfig@patch@l{paragraph}%
    \tabfig@patch@l{subparagraph}%
    \tabfig@patch@l{figure}%
    \tabfig@patch@l{table}%
%    \end{macrocode}
% Special support for parts and chapters in \pkg{memoir}.
%    \begin{macrocode}
    \@ifclassloaded{memoir}{%
      \tabfig@def\cftchapterpresnum{\tabfig@font \tabfig@\cftchapterpresnum}%
      \tabfig@def\cftpartpresnum{\tabfig@font \tabfig@\cftpartpresnum}%
    }{}%
  }%
}
%    \end{macrocode}
% 
% \subsection{Enumerations}
% Labels in enumerations.
%    \begin{macrocode}
\DeclareOption{enum}{%
  \AtBeginDocument{%
    \tabfig@def\labelenumi{\tabfig@font \tabfig@\labelenumi}%
    \tabfig@def\labelenumii{\tabfig@font \tabfig@\labelenumii}%
    \tabfig@def\labelenumiii{\tabfig@font \tabfig@\labelenumiii}%
    \tabfig@def\labelenumiv{\tabfig@font \tabfig@\labelenumiv}%
  }%
}
%    \end{macrocode}
% 
% \subsection{Bibliography}
% Labels in the bibliography.
%    \begin{macrocode}
\DeclareOption{bib}{%
  \AtBeginDocument{%
    \tabfig@def\@biblabel{\tabfig@font \tabfig@\@biblabel}%
    \@ifpackageloaded{biblatex}{%
      \DeclareFieldFormat{labelnumberwidth}{\mkbibbrackets{\tabfig@font #1}}%
      \DeclareFieldFormat{labelalphawidth}{\mkbibbrackets{\tabfig@font #1}}%
    }{}%
  }%
}
%    \end{macrocode}
%
% \subsection{Line numbers}
%    \begin{macrocode}
\DeclareOption{lineno}{%
  \AtBeginDocument{%
    \@ifpackageloaded{doc}{%
      \CheckCommand*\theCodelineNo{%
        \reset@font\scriptsize\arabic{CodelineNo}}%
      \def\theCodelineNo{%
        \reset@font\tabfig@font\scriptsize\arabic{CodelineNo}}%
    }{}%
  }%
}
\ProcessOptions\relax
%    \end{macrocode}
%
% \subsection{Auxiliary macros}
% This command is used for switching to tabular figures. This can be
% redefined to allow debugging, disabling, etc.
%    \begin{macrocode}
\newcommand*\tabfig@font{\tbfigures}
%    \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@font{\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}{%
    \PackageWarning{tabfigures}{There is no command
      \string\tbfigures\space to support tabular figures}%
    \let\tabfig@font\@empty
  }{}%
}
%</package>
%    \end{macrocode}
%
% \Finale
\endinput
