summaryrefslogtreecommitdiff
path: root/macros/latex/contrib/threeparttablex/threeparttablex.sty
blob: b186a9f68bf06adc5b15cf3bb8312b9071bb2194 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
\ProvidesPackage{threeparttablex}[2013/07/23 v0.3 by daleif]

%%
%% This package can be distributed and/or modified under the
%% conditions of the LaTeX Project Public License, either version 1.3
%% 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 2003/12/01 or later.
%% 
%% This work has the LPPL maintenance status "maintained".
%% 
%% The Current Maintainer of this work is Lars Madsen (daleif@imf.au.dk).
%%


\newif\ifTPTL@referable
\DeclareOption{referable}{\TPTL@referabletrue}
\DeclareOption*{\PassOptionsToPackage{\CurrentOption}{threeparttable}}

\ProcessOptions\relax

\RequirePackage{threeparttable}
\RequirePackage{environ}


\newcommand\TPTL@font{}
\newcommand\setTableNoteFont[1]{\renewcommand\TPTL@font{#1}}

% length to store the longtable width in
\newlength\TPTL@width
% macro to store the optional argument for tablenotes
\def\TPTL@optarg{}
% wrapper env used to collect the tablenotes for later use
\newenvironment{TableNotes}[1][]{
  \gdef\TPTL@optarg{#1}%
  \Collect@Body\TPTL@collector}{}
\newcommand\TPTL@collector[1]{%
  \long\gdef\TPTL@body{#1}}
% the macro used to store the tablenotes contents
\gdef\TPTL@body{}
\newcommand\insertTableNotes{%
  % first we need to know the width of the longtable, remember that it
  % will only settle after a few compilations
  \noalign{\begingroup
  \setlength\TPTL@width{0pt}
  \renewcommand\LT@entry[2]{\global\advance\TPTL@width by ##2}
  \@nameuse{LT@\romannumeral\c@LT@tables}
  \ifdim\TPTL@width<\TPTminimum\relax\global\TPTL@width=\TPTminimum\fi
  \endgroup}
  % then add the table notes inside a \parbox of the required width
  \multicolumn{\LT@cols}{c}{%
    \makebox[0pt][c]{%
      \parbox{\TPTL@width}{%
        \let\TPT@hsize\@empty% otherwise the width is not reset
                             % correctly as \TPT@hsize messes with 
                             % \@parboxrestore
        \let\TPToverlap\relax%
        \begin{tablenotes}[\TPTL@optarg]%
          \TPTL@font%
          \TPTL@body
        \end{tablenotes}
      }%
    }%
  }%
}

% the alternative threeparttable env, it only enables \TPToverlap
\newenvironment{ThreePartTable}{\let\TPToverlap\TPTrlap}{}



\ifTPTL@referable\else\endinput\fi

% we only come here if we need to make tablenotes referable


% define an extra \tnote command that table a label instead of a string
\DeclareRobustCommand\tnotex{\@ifstar{\TPTL@tnotex{01}}{\TPTL@tnotex{00}}}

\newcommand\TPTL@tnotex[2]{\tnote{\ref{#2}}}

% if hyperref is loaded we activate a starred version
\AtBeginDocument{%
  \@ifpackageloaded{hyperref}{%
    \renewcommand\TPTL@tnotex[2]{%
      \if#1\relax%
      \tnote{\ref{#2}}%
      \else%
      \tnote{\ref*{#2}}%
      \fi}}{}}


\newcommand\TPTL@generator[2]{%
  \providecommand{#1}{}% just to make it exist
  \renewcommand#1[1][#2]{%
    \let\TPTL@oldmakelabel=\makelabel%
    \renewcommand\makelabel[1]{\TPTLnotesnamefontcommand{##1:}}%
  \item%
    \let\makelabel=\TPTL@oldmakelabel}}

\newcommand\TPTL@generator@para[2]{%
  \providecommand{#1}{}% just to make it exist
  \renewcommand#1[1][#2]{%
    \TPTLnotesnamefontcommand{##1: }%
  }}


\providecommand\TPTLnotename{Note}
\providecommand\TPTLsourcename{Source}
\providecommand\TPTLnotesnamefontcommand{\textit}



\let\TPTL@tablenotes=\tablenotes

% \makelabel (i.e. \item) hides its argument inside a group, making it
% difficult to set \@currentlabel, so we locally overload \item to get
% the argument and set \@currentlabel AFTER \item[...]


\renewcommand\tablenotes{%
  \let\TPTL@item=\item
  \renewcommand\item[1][]{\TPTL@item[##1]%
    \phantomsection\protected@edef\@currentlabel{##1}}
  \TPTL@generator\note{\TPTLnotename}
  \TPTL@generator\source{\TPTLsourcename}
  \TPTL@font\TPTL@tablenotes}


% this is a copy from threeparttable, since adding to it failed
\def\TPT@doparanotes{\par
   \prevdepth\z@ \TPT@hsize
   \TPTnoteSettings
   \parindent\z@ \pretolerance 8
   \linepenalty 200
   \renewcommand\item[1][]{\relax\ifhmode \begingroup
       \unskip
       \advance\hsize 10em % \hsize is scratch register, based on real hsize
       \penalty -45 \hskip\z@\@plus\hsize \penalty-19
       \hskip .15\hsize \penalty 9999 \hskip-.15\hsize
       \hskip .01\hsize\@plus-\hsize\@minus.01\hsize 
       \hskip 1em\@plus .3em
      \endgroup\fi
      \tnote{##1}%
      \phantomsection\protected@edef\@currentlabel{##1}%
      \,\ignorespaces}%
    \let\TPToverlap\relax%
    \TPTL@generator@para\note{\TPTLnotename}%
    \TPTL@generator@para\source{\TPTLsourcename}%
    \TPTL@font%
    \def\endtablenotes{\par}%
  }



% just in case it is not already there
\providecommand\phantomsection{}


\endinput

The End