summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex/latex/easyformat/easyformat.sty
blob: ca3f6db334efdb9940526712beba629c09a901d3 (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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                                                                         %
%  easyformat  Easily add boldface, italics and smallcaps in LaTeX.       %
%  Copyright (C) 2017  Evert Provoost <evert.provoost@gmail.com>          %
%                                                                         %
%  This program is free software: you can redistribute it and/or modify   %
%  it under the terms of the GNU General Public License as published by   %
%  the Free Software Foundation, either version 3 of the License, or      %
%  (at your option) any later version.                                    %
%                                                                         %
%  This program is distributed in the hope that it will be useful,        %
%  but WITHOUT ANY WARRANTY; without even the implied warranty of         %
%  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          %
%  GNU General Public License for more details.                           %
%                                                                         %
%  You should have received a copy of the GNU General Public License      %
%  along with this program.  If not, see <http://www.gnu.org/licenses/>.  %
%                                                                         %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


%% NOTE TO FUTURE: Italics correction works because of \maybe@ic,
%                  this macro might become \xfss_maybe_ic: in LaTeX3
%
%                  In LaTeX3 the `New Font Selection Scheme' is replaced
%                  by xfss (xfont). Moving will require some research.


%% WAIT! This package doesn't use the LaTeX3 naming conventions?!
%      Yeah, since we make _ an active character, we can't. Sorry.


%% Administration...
\RequirePackage{expl3}[2015/11/11]
\ProvidesExplPackage
	{easyformat}
	{2017/05/28}
	{1.3.0}
	{Easily add boldface, italics and smallcaps.}


%% Public macros
% Add macros to switch the syntax on or off.
\cs_new:Npn \setundact {
	\char_set_catcode_active:N {\_}
}

\cs_new:Npn \setundsub {
	\char_set_catcode_math_subscript:N {\_}
}

\cs_new:Npn \setciract {
	\char_set_catcode_active:N {\^}
}

\cs_new:Npn \setcirsup {
	\char_set_catcode_math_superscript:N {\^}
}

\cs_new:Npn \enableeasyformat {
	\setundact
	\setciract
}

\cs_new:Npn \disableeasyformat {
	\setundsub
	\setcirsup
}

% Add macros which change the fontshape, fontseries and fontfamily.
\cs_new:Npn \setffamily #1 {
	\fontfamily{#1}
	\selectfont
}

\cs_new:Npn \setfshape #1 {
	\fontshape{#1}
	\selectfont
}

\cs_new:Npn \setfseries #1 {
	\fontseries{#1}
	\selectfont
}

% Add some macros which do small parts of what \normalfont does.
% Were needed in v1.1.0 but let's keep them because they are useful :)
\cs_new:Npn \nrfamily {
	\setffamily {\familydefault}
}

\cs_new:Npn \nrshape {
	\setfshape {\shapedefault}
}

\cs_new:Npn \nrseries {
	\setfseries {\seriesdefault}
}

% Add a macro LaTeX should have...
% The protected to fix the double superscript error.
\cs_new_protected:Npn \cir {\char_generate:nn {`^} {12}}


%% Constants & Variables
% Yes I could use \sb and \sp but those are dependencies...
\tl_const:Nx \EFOR@subscript   {\char_generate:nn {`_} {8}}
\tl_const:Nx \EFOR@superscript {\char_generate:nn {`^} {7}}

% Remember the style before changing to italics/boldface/smallcaps.
\str_new:N \EFOR@prev@shape@it
\str_new:N \EFOR@prev@shape@sc
\str_new:N \EFOR@prev@series@bf
\str_set:Nx \EFOR@prev@shape@it  {\shapedefault}  % Used by italics.
\str_set:Nx \EFOR@prev@shape@sc  {\shapedefault}  % Used by smallcaps.
\str_set:Nx \EFOR@prev@series@bf {\seriesdefault} % Used by boldface.


%% Formating handling
% Switch italics on or off.
\cs_new:Npn \EFOR@handle@italics {
	\str_if_eq:NNTF \f@shape \itdefault
	{
		\setfshape {\EFOR@prev@shape@it}
		\str_set:Nx \EFOR@prev@shape@it {\shapedefault}
		% \maybe@ic adds italics correction if needed.
		% (might stop working in LaTeX3?)
		\maybe@ic
	}{
		\str_set:Nx \EFOR@prev@shape@it {\f@shape}
		\itshape
	}
}

% Switch smallcaps on or off.
\cs_new:Npn \EFOR@handle@smallcaps {
	\str_if_eq:NNTF \f@shape \scdefault
	{
		\setfshape {\EFOR@prev@shape@sc}
		\str_set:Nx \EFOR@prev@shape@sc {\shapedefault}
	}{
		\str_set:Nx \EFOR@prev@shape@sc {\f@shape}
		\scshape
	}
}

% Switch boldface on or off.
\cs_new:Npn \EFOR@handle@boldface {
	\str_if_eq:NNTF \f@series \bfdefault
	{
		\setfseries {\EFOR@prev@series@bf}
		\str_set:Nx \EFOR@prev@series@bf {\seriesdefault}
	}{
		\str_set:Nx \EFOR@prev@series@bf {\f@series}
		\bfseries
	}
}


%% Aliases fo expl3
% Make aliases for some LaTeX3 macros as _ isn't allowed later on.
\let\Expl@mode@if@math\mode_if_math:TF
\let\Expl@cs@gset@protected\cs_gset_protected:Npn
\let\Expl@peek@meaning@remove\peek_meaning_remove:NTF


%% Activate the systems!
% First disable the Expl3 syntax as it will only cause trouble.
\ExplSyntaxOff

% Now enable the easyformat syntax.
\enableeasyformat

% Now finally add the new syntax in the document part.
\AtBeginDocument{
	% We do not re-enable easyformat here, since that could
	% cause unexpected behaviour. We tested if this causes errors.
	
	% NOTE: for some reason we have to use gset instead of new, no
	%       idea why though...
	
	% This is where the magic happens (also differentiates between
	% the text and math mode.)
	% It is protected because it should work in eg. \section{}

	\Expl@cs@gset@protected_{\Expl@mode@if@math
		{\EFOR@subscript}
		{\Expl@peek@meaning@remove_
			% The second underscore is removed when true.
			{\EFOR@handle@boldface}
			{\EFOR@handle@italics}%
		}%
	}

	\Expl@cs@gset@protected^{\Expl@mode@if@math
		{\EFOR@superscript}
		{\EFOR@handle@smallcaps}%
	}
}


%% Cleanup aka: Disable the systems!
\AtEndDocument{
	% Why no \def_\undefined and \def^\undefined?
	% Because if we use this syntax in eg. \section{},
	% the macro should still work in the .aux file...
	\disableeasyformat
}


%% DONE!
% Unfortunately we can't add a \disableeasyformat here,
% since that breaks more stuff than it fixes...


%% IMPLEMENTATION DETAILS:
%% Why not \textit/bf/sc\bgroup and \egroup?
%      Well actualy that was the original solution. However this
%      doesn't differentiate between ending itallics or ending
%      boldface. The current solution does and this makes the syntax
%      more predictable and generally more useful.