summaryrefslogtreecommitdiff
path: root/macros/latex/contrib/handout/handout.sty
blob: 88e407c870cdad89b8109a78c201c3a5f7fafbe6 (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
%% Copyright 2014… Maïeul Rouquette
%
% This work may 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 2005/12/01 or later.
%
% This work has the LPPL maintenance status `maintained'.
% 
% The Current Maintainer of this work is Maïeul Rouquette
% This work consists of the files handout.sty and example and documentation files.

\ProvidesPackage{handout}[2017/04/21 1.6.0 Handout package]
\RequirePackage{kvoptions,etoolbox,suffix}

% Options
\DeclareBoolOption[false]{disabled}
\let\disablehandout\handout@disabledtrue
\let\enablehandout\handout@disabledfalse

\DeclareBoolOption[false]{numbering}
\DeclareBoolOption[false]{sectioning}

\DeclareBoolOption[true]{printing}

\DeclareStringOption[.]{dir}

\ProcessKeyvalOptions*

% Warning tools
\newcommand{\handout@warning}[1]{\@latex@warning{Handout: #1}}

% Opening the handout file
\newwrite\@handout
\immediate\openout\@handout=\jobname.handout


% The main command, to be used by people
\newcommand{\handout}[1]{%	
	\ifhandout@disabled\else%Only if not disabled, of course!
		\ifhandout@numbering%
			\stepcounter{handoutnumber}%
			\handoutnumberintxt{\thehandoutnumber}%
		\fi%
		\ifhandout@printing%
		    \immediate\write\@handout {\string\handout@input{#1}}%
		\fi%
	\fi%
	\input{\handout@dir/#1}% Add the handout
}


% The special commands to be used by people

\newcommand{\nothandout}[1]{#1}
\newcommand{\onlyhandout}[1]{}
\newcommand{\forhandout}[1]{%
	\ifhandout@disabled\else%
	  \ifhandout@printing%
	    \immediate\write\@handout{\noexpand #1}%
	  \fi%
	\fi%
}
\WithSuffix\newcommand\forhandout*[1]{%
	\ifhandout@disabled\else%
	  \ifhandout@printing%
	    \immediate\write\@handout{\unexpanded{#1}}%
	  \fi%
	\fi%
}

% The command written in the .handout file
\newcommand{\handout@input}[1]{%
	\ifdefined\AtEveryHandout%
	  \AtEveryHandout%
	\fi%
	\ifhandout@numbering%
		\stepcounter{handoutnumber}%
		\handoutnumber{\thehandoutnumber}%
	\fi%
	\input{\handout@dir/#1}%
}
% Output the sectionning commands in the .handout file

\ifhandout@sectioning%Only if needed by option
	
	\apptocmd{\@sect}{% 
		\ifhandout@disabled\else%
		  \ifhandout@printing%
		    \immediate\write\@handout{\@backslashchar#1{\unexpanded{#8}}}%
		  \fi%
		\fi%
		}{}{\handout@warning{Can't patch sectioning commands}}

\fi
% Call the handout file at the end, if need

\AtEndDocument{%
    \ifhandout@printing%
	    \ifhandout@disabled\else
		  \immediate\closeout\@handout%
		  \before@handout%
		  \ifdef{\beforehandout}{\beforehandout}{\handout@warning{No command defined to be run before handout}}
		  \makeatletter%
		  \input\jobname.handout%
		  \makeatother%
		  \ifdef{\afterhandout}{\afterhandout}{\handout@warning{No command defined to be run after handout}}
	    \fi%
    \fi%
}

% Standard command before handout
\newcommand{\before@handout}{%
	% New page
	\newpage%
	% Reset counters
	\setcounter{page}{1}%
	\setcounter{footnote}{0}%
	\setcounter{table}{0}%
	\setcounter{figure}{0}%
	\ifhandout@sectioning%
		\setcounter{section}{0}%
		\setcounter{subsection}{0}%
		\setcounter{subsubsection}{0}%
	\fi
	\ifhandout@numbering
		\setcounter{handoutnumber}{0}%
	\fi
	% No new data in content or in label or index
	\def\addcontentsline##1##2##3{}
	\let\label\@gobble
	\renewcommand{\index}[2][1]{}
	 
	% Switches onlyhandout and nothandout commands
	\renewcommand{\nothandout}[1]{}
	\renewcommand{\onlyhandout}[1]{##1}

}

\AtBeginDocument{%
	\@ifpackageloaded{biblatex}{%
		\apptocmd{\before@handout}{\citereset}{}{}%
	}{}}

% Handout numbering

\ifhandout@numbering
	\newcounter{handoutnumber}
	\renewcommand{\thehandoutnumber}{\fbox{\arabic{handoutnumber}}}
\fi
\newcommand{\handoutnumberintxt}[1]{\handoutnumber{#1}}
\newcommand{\handoutnumber}[1]{\marginpar{#1}}