Debian Codegraph @

texmf-dist/source/latex/smart-eqn/smart-eqn.dtx

raw
1 % \iffalse meta-comment
2 %
3 % Copyright (C) 2021 by Ziyue "Alan" Xiang <[email protected]>
4 %
5 % This file may be distributed and/or modified under the conditions of
6 % the LaTeX Project Public License, either version 1.3c of this license
7 % 28 or (at your option) any later version. The latest version of this
8 % 29 license is in:
9 %
10 % http://www.latex-project.org/lppl.txt
11 %
12 % and version 1.3c or later is part of all distributions of LaTeX
13 % version 2006/05/20 or later.
14 %
15 % \fi
16 %
17 % \iffalse
18 %<*driver>
19 \ProvidesFile{smart-eqn.dtx}
20 %</driver>
21 %<*package>
22 \NeedsTeXFormat{LaTeX2e}
23 \ProvidesExplPackage{smart-eqn}{08/05/2021}{1.0}{Automatic Math Symbol Styling}
24 %</package>
25 %
26 %<*driver>
27 \makeatletter
28 \@namedef{[email protected]}{} % suppress the loading of fourier
29 \makeatother
30 \documentclass{ydoc}
31 \usepackage[english]{babel}
32 \usepackage{fontspec, unicode-math}
33 \usepackage{smart-eqn}
34 \usepackage{microtype}
35 \usepackage{etoolbox}
36 \usepackage{titlesec}
37 \usepackage{chngcntr}
38 \usepackage[skins,listings,xparse]{tcolorbox}
39 \usepackage{hyperref}
40
41 \ExplSyntaxOff
42
43 \AlsoImplementation
44
45 \setmainfont{ETbb-Regular}[
46 Extension=.otf,
47 BoldFont=ETbb-Bold,
48 ItalicFont=ETbb-Italic,
49 BoldItalicFont=ETbb-BoldItalic
50 ]
51 \setsansfont{QTAvanti}[
52 Extension=.otf,
53 ItalicFont=QTAvanti-Italic,
54 Scale=0.8
55 ]
56 \setmonofont{NewCMMono10-Regular}[
57 Extension=.otf,
58 BoldFont=NewCMMono10-Bold,
59 ItalicFont=NewCMMono10-Italic
60 ]
61 \newfontfamily{\QTFloraline}{QTFloraline}[
62 Extension=.otf,
63 BoldFont=QTFloraline-Bold
64 ]
65 \newfontfamily{\dejavumono}{DejaVuSansMono}[
66 Extension=.ttf,
67 BoldFont=DejaVuSansMono-Bold,
68 ItalicFont=DejaVuSansMono-Oblique,
69 BoldItalicFont=DejaVuSansMono-BoldOblique
70 ]
71 \setmathfont{Asana-Math}[Extension=.otf]
72
73 \newcommand{\thispkg}{\pkg{smart-eqn}}
74 \title{\QTFloraline The \thispkg~Package:\\ Automatic Math Symbol Styling}
75 \author{\href{https://www.alanshawn.com}{Ziyue ``Alan'' Xiang}}
76 \date{\today}
77 \repository{https://github.com/xziyue/smart-eqn}
78 \email{[email protected]}
79
80 \titleformat*{\section}{\QTFloraline\large\bfseries}
81 \titleformat*{\subsection}{\QTFloraline\bfseries}
82
83 %\newcounter{example}
84 %\counterwithin{example}{section}
85
86 \DeclareTCBListing{exbox}{soo}{
87 IfBooleanTF={#1}{listing only}{},
88 enhanced,
89 sharp corners,
90 boxrule=0pt,
91 attach boxed title to top left,
92 title={\sffamily\large EXAMPLE~\refstepcounter{example}\theexample%
93 \IfValueT{#2}{\label{#2}}%
94 \IfValueT{#3}{\space #3}
95 },
96 boxed title style={sharp corners, boxrule=0pt, colback=black!25},
97 listing options={
98 basicstyle={\dejavumono\scriptsize},
99 xleftmargin=0pt,
100 xrightmargin=0pt,
101 framesep=0pt,
102 aboveskip=0pt,
103 belowskip=0pt,
104 framexbottommargin=0pt,
105 breaklines=true,
106 breakatwhitespace=false
107 }
108 }
109
110 \def\PrintMacroCode{%
111 \begingroup
112 \dejavumono
113 \fontsize{6}{8}\selectfont
114 \noindent\themacrocode
115 \par
116 \endgroup
117 }
118 \def\linenumberbox{%
119 \hbox to 1.25em{}%
120 \llap{%
121 \stepcounter{linenumber}%
122 {\color{gray}\tiny\thelinenumber~}%
123 }%
124 }
125 \ExplSyntaxOn
126 \def\PrintMacroImplName#1{%
127 \str_set:Nn \l_tmpa_str {#1}
128 \regex_replace_all:nnN {\s} {} \l_tmpa_str
129 \implstyle{\str_use:N \l_tmpa_str}%
130 }
131 \ExplSyntaxOff
132
133 \begin{document}
134 \DocInput{smart-eqn.dtx}
135 \end{document}
136 %</driver>
137 % \fi
138 % \maketitle
139 % \tableofcontents
140 % \section{Introduction}
141 % \makeatmath
142 % \smesetsym{symbfup}{A,v}
143 % In \LaTeX~typesetting, it is usually the case that one needs to use different variants of a math symbol to clarify the meanings. For example, in linear algebra literature, it is common to use boldfaced symbols to represent vectors, and use normal symbols to represent scalars, which makes equations like @Av=\lambda v@ easier to understand. However, applying these variants by typing |\mathbf|, |\mathrm| commands manually can be daunting. The \thispkg~package aims to provide an automatic and customizable approach for math symbol styling, which eliminates the need to enter style commands repeatedly.
144 % \makeatother
145
146 %\section{Usage}
147
148 %\subsection{Style Configuration}
149
150 % \DescribeMacro\smesetsym{<style csname>}{<symbols>}
151
152 % The \meta{symbols} given in the form of comma separated list will be styled using \meta{style csname}. In Example \ref{ex:set-style}, the four symbols $A$, $v$, $Q$, and $\Lambda$ will be styled with the command |\symbfup| from the \pkg{unicode-math} package, which produces boldfaced symbols. If traditional \LaTeX~is used, one can use the |\mathbf| command instead.
153
154
155 % \DescribeMacro\smeclearsym
156
157 % Clear the style configuration associated with all symbols.
158
159
160 % \subsection{Inline Math}
161
162 % \makeatletter
163 % In traditional \LaTeX, inline math is typeset with |$...$|. In \thispkg, we use |@...@| instead. For inline math demonstrations, see Examples \ref{ex:inl-1}, \ref{ex:inl-2}, \ref{ex:inl-3}, \ref{ex:inl-4}.
164
165 % \DescribeMacro\makeatmath
166
167 % The command changes the category code of |@| so that it can be used as the inline math environment. To revert the change, one can use |\makeatletter| or |\makeatother|.
168
169 % \subsection{Display Math}
170
171 % In order to use display math environments, we need to declare smart math environments using |\smenewenv|. The names of new math environments will be prefixed with ``sme''. The way of passing arguments to the math environment (e.g., \env{alignat}) will also be slightly different. For display math demonstrations, see Examples \ref{ex:disp-1}, \ref{ex:disp-2}, \ref{ex:disp-3}, \ref{ex:disp-4}, \ref{ex:disp-5}.
172
173 % \DescribeMacro\smenewenv{<env name>}
174
175 % Declares a ``smart'' math environment based on the traditional math environment provided in \meta{env name}. The name of the new math environment will be prefixed with ``sme''. For example, if one calls |\smenewenv{align}|, then the smart math environment \env{smealign} will be available.
176 %\paragraph{Passing arguments} The arguments for math environments needed to be enclosed in square brackets. See Example \ref{ex:disp-5}.
177
178 % \subsection{Raw Math Content}
179
180 % \DescribeMacro\smeraw{<content>}
181 % The \meta{content} of |\smeraw| will not be styled. This can be useful when one needs to style symbols manually in \thispkg~environments. See Examples \ref{ex:inl-4}, \ref{ex:disp-4}.
182
183 % \section{Examples}
184
185 % The following code snippet is executed before running the examples:
186 % \iffalse
187 %<*example>
188 % \fi
189 \begin{tcblisting}{listing only, sharp corners, boxrule=0pt}
190 \makeatmath
191 \smenewenv{align}
192 \smenewenv{gather}
193 \smenewenv{alignat}
194 \end{tcblisting}
195 % \iffalse
196 %</example>
197 % \fi
198
199 % \makeatmath
200 % \smenewenv{align}
201 % \smenewenv{gather}
202 % \smenewenv{alignat}
203
204 % \iffalse
205 %<*example>
206 % \fi
207 \begin{exbox}*[ex:set-style][(Setting styles for symbols)]
208 \smesetsym{symbfup}{A, v, Q, \Lambda}
209 \end{exbox}
210 % \iffalse
211 %</example>
212 % \fi
213
214
215 % \iffalse
216 %<*example>
217 % \fi
218 \begin{exbox}[ex:inl-1][(Inline math example 1)]
219 \smesetsym{symbfup}{A, v}
220 The eigenvalue @\lambda@ and eigenvector @v@ of matrix @A@ satisfy @Av=\lambda v@.
221 \end{exbox}
222 % \iffalse
223 %</example>
224 % \fi
225
226 % \iffalse
227 %<*example>
228 % \fi
229 \begin{exbox}[ex:inl-2][(Inline math example 2)]
230 \smesetsym{symbfup}{Q, \Lambda, I}
231 A symmetric matrix @A@ can be orthogonally diagonalized, that is, @A=Q\Lambda Q^T@, where @Q^TQ=I@ and @\Lambda@ is diagonal.
232 \end{exbox}
233 % \iffalse
234 %</example>
235 % \fi
236
237 % \iffalse
238 %<*example>
239 % \fi
240 \begin{exbox}[ex:inl-3][(Inline math example 3)]
241 \smesetsym{mathcal}{S}
242 For a set @S@, the power set of @S@ (denoted by @2^S@) is the set of all subsets of @S@.
243 \end{exbox}
244 % \iffalse
245 %</example>
246 % \fi
247
248 % \iffalse
249 %<*example>
250 % \fi
251 \begin{exbox}[ex:inl-4][(Inline math example 4)]
252 I just want to juxtapose @S \smeraw{S} \smeraw{\symbfup{S}} \smeraw{\symbfit{S}} \smeraw{\mathrm{S}}@.
253 \end{exbox}
254 % \iffalse
255 %</example>
256 % \fi
257
258 % \iffalse
259 %<*example>
260 % \fi
261 \begin{exbox}[ex:disp-1][(Display math example 1)]
262 \begin{smealign}
263 Av_1&=\lambda_1 v_1\\
264 Av_2&=\lambda_2 v_2
265 \end{smealign}
266 \end{exbox}
267 % \iffalse
268 %</example>
269 % \fi
270
271 % \iffalse
272 %<*example>
273 % \fi
274 \begin{exbox}[ex:disp-2][(Display math example 2)]
275 \begin{smegather}
276 k_1v_1 + k_2v_2 + \cdots + k_n v_n = \symbfup{0}\\
277 l_1v_1 + l_2v_2 + \cdots + l_{n-1} v_{n-1} = \symbfup{1}
278 \end{smegather}
279 \end{exbox}
280 % \iffalse
281 %</example>
282 % \fi
283
284 % \iffalse
285 %<*example>
286 % \fi
287 \begin{exbox}[ex:disp-3][(Display math example 3)]
288 \begin{smealign}
289 e^{At} = I+\sum_{n=1}^{\infty} \frac{A^n t^n}{n!}
290 \end{smealign}
291 \end{exbox}
292 % \iffalse
293 %</example>
294 % \fi
295
296 % \iffalse
297 %<*example>
298 % \fi
299 \begin{exbox}[ex:disp-4][(Display math example 4)]
300 \begin{smealign}
301 S
302 \smeraw{S}
303 \smeraw{\symbfup{S}}
304 \smeraw{\symbfit{S}}
305 \smeraw{\mathrm{S}}
306 \end{smealign}
307 \end{exbox}
308 % \iffalse
309 %</example>
310 % \fi
311
312
313
314 % \iffalse
315 %<*example>
316 % \fi
317 \begin{exbox}[ex:disp-5][(Display math example 5)]
318 \smesetsym{symbfup}{x,b}
319 \begin{smealignat}[{5}]
320 &&\phantom{\Rightarrow} && (c_1^2 + \cdots + c_n^2) \lambda_n^{-2} &\leq \lVert \delta x \rVert^2 &&\leq (c_1^2 + \cdots + c_n^2) \lambda_1^{-2}\\
321 &&\Rightarrow &&\lVert \delta b \rVert^2\lambda_n^{-2} &\leq \lVert \delta x \rVert^2 &&\leq \lVert \delta b \rVert^2\\
322 &&\Rightarrow &&\lVert \delta b \rVert^2\lambda_n^{-2} &\leq \lVert \delta x \rVert^2 &&\leq \lVert \delta b \rVert^2
323 \end{smealignat}
324 \end{exbox}
325 % \iffalse
326 %</example>
327 % \fi
328
329 % \StopEventually{}
330 % \makeatother
331
332 % \section{Implementation}
333 % \begin{macrocode}
334 \RequirePackage{fancyvrb}
335 \RequirePackage{expl3, xparse}
336
337 \makeatletter
338 \ExplSyntaxOn
339
340 \tl_new:N \g_sme_op_arg_tl
341 \ior_new:N \g_sme_tmpa_ior
342 \iow_new:N \g_sme_tmpa_iow
343 \prop_new:N \g_sme_symbols_prop
344 \clist_new:N \l_sme_tmpa_clist
345 \tl_new:N \l_sme_tmpa_tl
346 \tl_new:N \l_sme_tmpb_tl
347 \tl_new:N \l_sme_tmpc_tl
348 \tl_new:N \l_sme_cur_math_tl
349 % \end{macrocode}
350
351 % \begin{macro}{\smeDefineVerbatimEnvironment}
352 % This is a modified version of |\DefineVerbatimEnvironment| from \pkg{fancyvrb}. In this function, |\smeFV@Environment| is used insetad of |\FV@Environment|.
353 % \begin{macrocode}
354 \def\smeDefineVerbatimEnvironment#1#2#3{%
355 \@namedef{#1}{\smeFV@Environment{#3}{#2}}%
356 \@namedef{end#1}{\@nameuse{FVE@#2}}%
357 }
358 % \end{macrocode}
359 % \end{macro}
360
361 % \begin{macro}{\smeFV@Environment}
362 % This is a modified version of |\FV@Environment| from \pkg{fancyvrb}. In this function, |\smeFV@GetKeyValues| is used instead of |\FV@GetKeyValues|.
363 % \begin{macrocode}
364 \def\smeFV@Environment#1#2{%
365 \def\FV@KeyValues{#1}%
366 \catcode`\^^M=\active
367 \tl_gclear:N \g_sme_op_arg_tl % clear optional arguments from previous calls
368 \@ifnextchar[%
369 {\catcode`\^^M=5 \smeFV@GetKeyValues{\@nameuse{FVB@#2}}}%
370 {\catcode`\^^M=5 \@nameuse{FVB@#2}}}
371 % \end{macrocode}
372 % \end{macro}
373
374 % \begin{macro}{\smeFV@GetKeyValues}
375 % This is a modified version of |FV@GetKeyValues| from \pkg{fancyvrb}. We directly save the optional parameters captured by \pkg{fancyvrb} to the variable |\g_sme_op_arg_tl|, which can be used later.
376 % \begin{macrocode}
377 \def\smeFV@GetKeyValues#1[#2]{%
378 \tl_gset:Nn \g_sme_op_arg_tl {#2}#1}
379 % \end{macrocode}
380 % \end{macro}
381
382 % \begin{macro}{\sme_declare_math_env:n}[1]{math environment name}
383 % This function declares a ``smart'' math environment. It is based on the \env{VerbatimOut} environment from \pkg{fancyvrb}, which writes its content out to the file |\jobname-sme.vrb|. The content stored in the external file will be read back and processed.
384 % \begin{macrocode}
385 \cs_set:Npn \sme_declare_math_env:n #1 {
386 \exp_args:Nc \def{sme#1}{\smeFV@Environment{}{sme#1}}
387
388 % this is a modified version of VerbatimOut environment which does not take any argument
389 \exp_args:Nc \def{FVB@sme#1}{%
390 \@bsphack
391 \begingroup
392 \FV@UseKeyValues
393 \FV@DefineWhiteSpace
394 \def\FV@Space{\space}%
395 \FV@DefineTabOut
396 \def\FV@ProcessLine{\immediate\write\FV@OutFile}%
397 \immediate\openout\FV@OutFile \jobname-sme.vrb\relax
398 \let\FV@FontScanPrep\relax
399 %% DG/SR modification begin - May. 18, 1998 (to avoid problems with ligatures)
400 \let\@noligs\relax
401 %% DG/SR modification end
402 \FV@Scan}
403
404 \exp_args:Nc\def{FVE@sme#1}{\immediate\closeout\FV@OutFile\endgroup\@esphack
405 % call the function to process the content when the environment ends
406 \sme_read_process_math:n {#1}
407 }
408
409 \smeDefineVerbatimEnvironment{sme#1}{sme#1}{}
410 }
411
412 % define the user function
413 \let\smenewenv\sme_declare_math_env:n
414 % \end{macrocode}
415 % \end{macro}
416
417 % \begin{macro}{\sme_read_process_math:n}[1]{math environment name}
418 % Read back and process the math content stored in the external file |\jobname-sme.vrb|. After processing, the new content will be stored in another file |\jobname-sme-in.vrb|. Eventually, the new content will be fed into \LaTeX~using |\input|.
419 % \begin{macrocode}
420 \cs_set:Npn \sme_read_process_math:n #1 {
421 \ior_open:Nn \g_sme_tmpa_ior {\jobname-sme.vrb}
422 \iow_open:Nn \g_sme_tmpa_iow {\jobname-sme-in.vrb}
423
424 \iow_now:Nx \g_sme_tmpa_iow {\c_backslash_str begin{#1} \g_sme_op_arg_tl}
425 \ior_map_inline:Nn \g_sme_tmpa_ior {
426 \sme_process_math:n {##1}
427 \exp_args:NNV \iow_now:Nn \g_sme_tmpa_iow \l_sme_cur_math_tl
428 }
429 \iow_now:Nx \g_sme_tmpa_iow {\c_backslash_str end{#1}}
430 \iow_close:N \g_sme_tmpa_iow
431 \ior_close:N \g_sme_tmpa_ior
432 \input{\jobname-sme-in.vrb}
433 }
434 % \end{macrocode}
435 % \end{macro}
436
437 % \begin{macro}{\smesetsym}
438 % stores the style information in |\g_sme_symbols_prop|
439 % \begin{macrocode}
440 \newcommand{\smesetsym}[2]{
441 \clist_set:Nn \l_sme_tmpa_clist {#2}
442 \clist_map_inline:Nn \l_sme_tmpa_clist {
443 \prop_gput:Nnn \g_sme_symbols_prop {##1} {#1}
444 }
445 }
446 % \end{macrocode}
447 % \end{macro}
448
449 % \begin{macro}{\smeclearsym}
450 % clear the style information stored in |\g_sme_symbols_prop|
451 % \begin{macrocode}
452 \newcommand{\smeclearsym}{
453 \prop_gclear:N \g_sme_symbols_prop
454 }
455 % \end{macrocode}
456 % \end{macro}
457
458 % \begin{macro}{\smeraw}
459 % used to apply custom styles in smart-eqn environments
460 % \begin{macrocode}
461 \newcommand{\smeraw}[1]{#1}
462 % \end{macrocode}
463 % \end{macro}
464
465 % \begin{macro}{\__sme_grouped:n}
466 % \begin{macrocode}
467 \cs_set:Npn \__sme_grouped:n #1 {
468 \exp_not:n {#1}
469 }
470 \cs_generate_variant:Nn \__sme_grouped:n {e}
471 \cs_generate_variant:Nn \__sme_grouped:n {V}
472 % \end{macrocode}
473 % \end{macro}
474
475 % \begin{macro}{\sme_construct_bm:Nn}[2]{result variable}{current token list}
476 % This function is the core of automatic math styling. It examines each token in \meta{current token list} and determines if it needs special styling. The function runs recursively and stores the result in \meta{result variable}.
477 % \begin{macrocode}
478 \cs_set:Npn \sme_construct_bm:Nn #1#2 {
479 \tl_if_head_is_group:nTF {#2} {
480 % if head is group, apply the algorithm recursively
481 %\tl_put_right:Nx #1 {{\tl_head:n {#2}}}
482 \tl_put_right:NV #1 \c_left_brace_str
483 \exp_args:NNx \sme_construct_bm:Nn #1 {\tl_head:n {#2}}
484 \tl_put_right:NV #1 \c_right_brace_str
485 \tl_if_empty:nF {#2} {\exp_args:NNx \sme_construct_bm:Nn #1 {\tl_tail:n {#2}}}
486 }{
487 \tl_if_head_is_space:nTF {#2} {
488 % ignore spaces
489 \exp_args:NNx \sme_construct_bm:Nn #1 {\tl_trim_spaces:n {#2}}
490 } {
491 \tl_if_head_eq_meaning:nNTF {#2} \smeraw {
492 % for \smeraw, ignore the next group
493 \tl_put_right:Nn #1 {\smeraw}
494 \tl_set:Nx \l_sme_tmpa_tl {\tl_tail:n {#2}}
495 \tl_put_right:Nx #1 {\tl_head:N \l_sme_tmpa_tl}
496 \tl_set:Nx \l_sme_tmpa_tl {\tl_tail:N \l_sme_tmpa_tl}
497 \exp_args:NNV \sme_construct_bm:Nn #1 \l_sme_tmpa_tl
498 } {
499 % common case
500 % extract head
501 \exp_args:NNx \prop_get:NnNTF \g_sme_symbols_prop {\tl_head:n {#2}} \l_sme_tmpc_tl {
502 % this symbol needs to be styled
503 \tl_set:No \l_sme_tmpa_tl {\csname\l_sme_tmpc_tl\endcsname}
504 \tl_put_right:Nx \l_sme_tmpa_tl {{\__sme_grouped:e {\tl_head:n {#2}}}}
505 \tl_put_right:Nx #1 {{\__sme_grouped:V \l_sme_tmpa_tl}}
506 } {
507 % otherwise, use the original symbol
508 \tl_put_right:Nx #1 {\tl_head:n {#2}}
509 }
510 \tl_if_empty:nF {#2} {\exp_args:NNx \sme_construct_bm:Nn #1 {\tl_tail:n {#2}}}
511 }
512 }
513 }
514 }
515 % \end{macrocode}
516 % \end{macro}
517
518 % \begin{macro}{\sme_process_math:n}[1]{inline content}
519 % used by inline math environment to process the content
520 % \begin{macrocode}
521 \cs_set:Npn \sme_process_math:n #1 {
522 \tl_clear:N \l_sme_cur_math_tl
523 \sme_construct_bm:Nn \l_sme_cur_math_tl {#1}
524 }
525 % \end{macrocode}
526 % \end{macro}
527
528 % \begin{macro}{\makeatmath}
529 % changes the catcode and definition of |@| so that we can use it for math typesetting
530 % \begin{macrocode}
531 \begingroup
532 \catcode`@=\active
533 \gdef\makeatmath{% note the global \gdef
534 \catcode`@=\active
535 \def@##1@{
536 \sme_process_math:n{##1}
537 $\exp_args:NnV \tl_rescan:nn {} \l_sme_cur_math_tl$
538 }%
539 }
540 \endgroup
541 % \end{macrocode}
542 % \end{macro}
543
544 % \begin{macrocode}
545 \makeatother
546 \ExplSyntaxOff
547 % \end{macrocode}
548
549 %\Finale
550 \endinput