Debian Codegraph @

texmf-dist/source/latex/xfrac/xfrac.dtx

raw
1 % \iffalse meta-comment
2 %
3 %% File: xfrac.dtx
4 %
5 % Copyright (C) 2004,2008-2010 Morten Hoegholm
6 % (C) 2011,2012,2014-2024 The LaTeX Project
7 %
8 % It may be distributed and/or modified under the conditions of the
9 % LaTeX Project Public License (LPPL), either version 1.3c of this
10 % license or (at your option) any later version. The latest version
11 % of this license is in the file
12 %
13 % https://www.latex-project.org/lppl.txt
14 %
15 % This file is part of the "xfrac package" (The Work in LPPL)
16 % and all files in that bundle must be distributed together.
17 %
18 % -----------------------------------------------------------------------
19 %
20 % The development version of the bundle can be found at
21 %
22 % https://github.com/latex3/xfrac
23 %
24 % for those people who are interested.
25 %
26 %<*driver|package>
27 \RequirePackage{amstext,graphicx}
28 %</driver|package>
29 %<*driver>
30 \documentclass[full]{l3doc}
31 \usepackage{nicefrac,xfrac} ^^A Need nicefrac for demo purposes
32 \providecommand*\key[1]{\textbf{#1}}
33 \newcommand*\switch[2]{{\fontfamily{#1}\selectfont #2}}
34 \begin{document}
35 \DocInput{\jobname.dtx}
36 \end{document}
37 %</driver>
38 % \fi
39 %
40 % \title{^^A
41 % The \pkg{xfrac} package\\ Split-level fractions^^A
42 % }
43 %
44 % \author{^^A
45 % The \LaTeX{} Project\thanks
46 % {^^A
47 % E-mail:
48 % \href{mailto:[email protected]}
49 % {[email protected]}^^A
50 % }^^A
51 % }
52 %
53 % \date{Released 2024-06-04}
54 %
55 % \maketitle
56 %
57 % \begin{documentation}
58 %
59 % The \pkg{xfrac} package defines a document command \cs{sfrac}
60 % with the following syntax:
61 % \begin{quote}
62 % \cs{sfrac}\oarg{instance}\marg{num}\oarg{sep}\marg{denom}
63 % \end{quote}
64 % Let's show a few examples:
65 % \begin{verbatim}
66 % \sfrac{1}{2}, $\sfrac{1}{2}$,
67 % $\mathbf{3\times\sfrac{1}{2}}$
68 % \quad \fontfamily{ppl}\selectfont Palatino: \sfrac{1}{2}
69 % \quad \fontfamily{ptm}\selectfont Times: \sfrac{1}{2}
70 % \end{verbatim}
71 % \begin{quote}
72 % \sfrac{1}{2}, $\sfrac{1}{2}$, $\mathbf{3\times\sfrac{1}{2}}$
73 % \quad \fontfamily{ppl}\selectfont Palatino: \sfrac{1}{2}
74 % \quad \fontfamily{ptm}\selectfont Times: \sfrac{1}{2}
75 % \end{quote}
76 % You'll notice something interesting: not only does the \cs{sfrac}
77 % command work as it should in math mode, it also gets the job done
78 % for other fonts as well.
79 %
80 % \section{A Bit of History}
81 %
82 % \subsection{The Past}
83 %
84 % One of the first exercises in \emph{The \TeX{}Book} is to design a
85 % macro for split level fractions. The solution presented is fairly
86 % simple, using a \emph{virgule} (a slash) for separating the two
87 % components. It looks okay because the text font and math font of
88 % Computer Modern look almost identical.
89 %
90 % The proper symbol to use instead of the virgule is a \emph{solidus}
91 % which does not exist in Computer Modern. It is however available in
92 % the European Computer Modern fonts, but I'll get back to that.
93 %
94 % \subsection{The Present}
95 %
96 % The most common way to produce split level fractions within \LaTeXe{}
97 % is by means of the \pkg{nicefrac} package. Part of the reason it
98 % has found widespread use is due to the strange design of the
99 % built-in text fractions of the EC fonts, which look like this:
100 % \textonehalf{}. The package is very simple to use but there are a few
101 % issues:
102 % \begin{itemize}
103 % \item It uses the virgule instead of the solidus.
104 % \item Font size of numerator and denominator is bigger than in the
105 % built-in symbol. Compare Palatino: \switch{ppl}{\nicefrac{1}{2}}
106 % \emph{vs.}~\switch{ppl}{\textonehalf}.
107 % \item It doesn't correct for fonts using text figures such as in the
108 % \pkg{eco} package. Compare \switch{cmor}{\nicefrac{1}{2}} and
109 % \switch{cmor}{\nicefrac{8}{9}}.
110 % \item In math mode, it doesn't always pick up the correct math
111 % alphabet.
112 % \end{itemize}
113 % In short: \pkg{nicefrac} doesn't attempt to be the answer to
114 % everything and so this is not a criticism of the package. It works
115 % quite well for Computer Modern which was pretty much what was widely
116 % available at the time it was developed. Users these days, however,
117 % have a choice of many fonts when they write their documents.
118 %
119 % \subsection{The Future}
120 %
121 % Fonts are wildly different; one macro that works fine for Computer
122 % Modern obviously doesn't work well at all in Palatino. For one we
123 % have to make the separator symbol configurable, and we need to
124 % take care of several details as well: font scaling of the
125 % numerator/denominator pair (ND), font selection of ND, \emph{etc.} If we
126 % are to have a single package for this in the future we have to define a
127 % totally generic interface for the fraction commands and then adjust
128 % parameters depending on the current font. What you see in this
129 % prototype implementation of \pkg{xfrac} is just that.
130 %
131 % \section{Advanced User Interface}
132 %
133 % \subsection{Text mode}
134 %
135 % The usual problem in text mode has a name: Computer Modern. The
136 % solidi of all the Computer Modern fonts leave a lot to be desired,
137 % although things are potentially looking better as the Latin Modern
138 % fonts are becoming more stable and widespread. As long as the
139 % default fonts are Computer Modern variants we must however work
140 % around this. One idea that comes to mind is to see what happens
141 % when you use a solidus from another font instead. Let's try with
142 % Times:
143 % \begin{quote}
144 % \DeclareInstance{xfrac}{cmr2}{text}
145 % {slash-symbol-font = ptm}
146 % \enquote{You take \sfrac[cmr2]{1}{2} cup of sugar, \ldots}
147 % \end{quote}
148 % That looks quite good actually, so it was probably very difficult
149 % to obtain that result. Nope, it was extremely easy---if you happen
150 % to know about \emph{instances}:
151 % \begin{verbatim}
152 % \DeclareInstance{xfrac}{cmr}{text}
153 % {slash-symbol-font = ptm}
154 % \end{verbatim}
155 % So we define an instance with the name |cmr| from the template
156 % |text| which in turn is of object type |xfrac|. You'll notice
157 % the |cmr| is also the name of the font family for Computer Modern
158 % Roman and the reasoning behind is that every font family should
159 % have it's own settings, and if a document command is to work well
160 % in that scheme, letting it use the name of the current font family
161 % seems like a good idea. Thus the \cs{sfrac} command checks to see
162 % whether an instance with same name as the current font family
163 % exists and uses it if the test is true; otherwise the default
164 % setting is used. Here we defined the instance to be used for the
165 % font family |cmr| and just told it to use the Times font for
166 % typesetting the slash symbol which turns out to be a solidus by
167 % default.
168 %
169 % The option \texttt{cm-recommended} which is loaded by default uses
170 % the Times solidus for Computer Modern Roman and Computer Modern
171 % Sans Serif and the Palatino solidus for Computer Modern Typewriter
172 % Type. This looks quite good. Should you however not want this you
173 % can use the option \texttt{cm-standard} which produces somewhat
174 % acceptable results using Computer Modern exclusively.
175 %
176 % So what about old style figures? If you use the \pkg{eco}
177 % package you might define an instance similar to this (`cmor' is
178 % the name of the roman font activated by \pkg{eco}):
179 % \DeclareInstance{xfrac}{cmor}{text}
180 % {
181 % slash-symbol-font = ptm,
182 % numerator-font = cmr,
183 % denominator-font = cmr
184 % }
185 % \begin{verbatim}
186 % \DeclareInstance{xfrac}{cmor}{text}
187 % {
188 % slash-symbol-font = ptm,
189 % numerator-font = cmr,
190 % denominator-font = cmr
191 % }
192 % \end{verbatim}
193 % We also use regular Computer Modern Roman for typesetting ND, so
194 % we end up with \switch{cmor}{\sfrac{1}{2}} and
195 % \switch{cmor}{\sfrac{8}{9}} instead of
196 % \switch{cmor}{\nicefrac{1}{2}} and \switch{cmor}{\nicefrac{8}{9}}.
197 % Much better.
198 %
199 % There are also situations where other tricks are useful. If you
200 % don't have the inferior and superior figures available in a font,
201 % or the font doesn't have a wider design for small font sizes, you
202 % can cheat by manually scaling the ND-pair. I got nice results for
203 % Adobe's Stempel Garamond (with small caps and old style figures)
204 % with the following setup:
205 % \begin{verbatim}
206 % \DeclareInstance{xfrac}{pegj}{text}
207 % {
208 % numerator-font = pegx,
209 % denominator-font = pegx,
210 % scale-factor = 0.9,
211 % h-scale = 1.1
212 % }
213 % \end{verbatim}
214 % We use the font family |pegx| (Stempel Garamond with real small
215 % caps) for typesetting the ND-pair. Additionally the key
216 % \key{scale-factor} specifies that the font size used for the
217 % ND-pair should be $0.9$ of the height of the solidus, and the key
218 % \key{h-scale} specifies that the ND-pair should be scaled an extra
219 % 10\,\% horizontally.
220 %
221 % Should you be so fortunate the have a font with inferior and
222 % superior figures like in the Monotype Janson example from Philipp
223 % Lehman's excellent \emph{The Font Installation Guide}. In that
224 % example Philipp defines the font families |mjn0| for the inferior
225 % figures and |mjn1| for the superior. Thus to get the \cs{sfrac}
226 % command to use them on the fly for the font family |mjnj| (Janson,
227 % old style figures) we would say
228 % \begin{verbatim}
229 % \DeclareInstance{xfrac}{mjnj}{text}
230 % {
231 % numerator-font = mjn1,
232 % denominator-font = mjn0,
233 % scaling = false,
234 % numerator-bot-sep = 0 pt,
235 % denominator-bot-sep = 0 pt
236 % }
237 % \end{verbatim}
238 % I think this example is a very clean way to do it. An alternative
239 % approach could be to use the keys \key{numerator-format} and
240 % \key{denominator-format} to process the arguments and let them
241 % determine what to do.
242 %
243 % \subsection{Math Mode}
244 %
245 % In math mode the choices are a lot fewer because first of all
246 % \TeX{} comes with a built-in limitation of $16$ math families.
247 % Additionally we will not need a solidus for typesetting split
248 % fractions in math, as tradition is to use a virgule instead. We
249 % define the basic |mathdefault| instance to simply use the math
250 % family in use when the instance is run. So if we're in normal math
251 % like |$\sfrac{7}{9}$| we simply get family~$-1$. If we're inside a
252 % \cs{mathbf} we're in family~$4$ (in the standard setup at least),
253 % and so the fraction is typeset with the same math family. Simple,
254 % isn't?
255 %
256 % You can also declare instances for the math families, but I really
257 % don't see why you would. If you do then name them according to the
258 % scheme \texttt{mathfam\meta{N}}, where \meta{N} is the family number, and
259 % only do it if you \emph{really} know how to set up math fonts.
260 % That is, if \cs{DeclareMathAlphabet} is unbeknownst to you, then
261 % just don't go there.
262 %
263 % \section{The Template Interface}
264 %
265 % \begin{TemplateInterfaceDescription}{xfrac}
266 %
267 % \TemplateArgument{1}{The numerator}
268 %
269 % \TemplateArgument{2}{The separator}
270 %
271 % \TemplateArgument{3}{The denominator}
272 %
273 % \TemplateSemantics
274 %
275 % Typesets arguments 1 and 3 separated by argument 2, which in text
276 % mode by default is a \emph{solidus}. This is taken from
277 % \pkg{textcomp} where it is denoted \cs{textfractionsolidus}.
278 % This is the character used for the ready made split level
279 % fractions such as \textonehalf{}---except in the (European) Computer
280 % Modern fonts. In math mode a \emph{virgule} is used instead as
281 % this is more appropriate and it is always available in the math
282 % fonts. The solidus is a text symbol only.
283 %
284 % \end{TemplateInterfaceDescription}
285 %
286 % \begin{TemplateDescription}{xfrac}{text}
287 %
288 % \TemplateKey{numerator-font}{tokenlist}
289 % {Font family specification to use for the numerator.}
290 % {\cs{f@family}}
291 %
292 % \TemplateKey{numerator-format}{function 1 arg}
293 % {Action to be taken on the numerator.}
294 % {Process argument unchanged}
295 %
296 % \TemplateKey{slash-symbol}{tokenlist}
297 % {The separator symbol. If not specified the default value will be
298 % used instead.}
299 % {Solidus (\cs{textfractionsolidus})}
300 %
301 % \TemplateKey{slash-symbol-font}{tokenlist}
302 % {Font family specification to use for the separator symbol.}
303 % {\cs{f@family}}
304 %
305 % \TemplateKey{slash-symbol-format}{function 1 arg}
306 % {Action to be taken on the separator symbol.}
307 % {Process argument unchanged}
308 %
309 % \TemplateKey{denominator-font}{tokenlist}
310 % {Font family specification to use for the denominator.}
311 % {\cs{f@family}}
312 %
313 % \TemplateKey{denominator-format}{function 1 arg}
314 % {Action to be taken on the denominator.}
315 % {Process argument unchanged}
316 %
317 % \TemplateKey{h-scale}{real}
318 % {Factor by which the numerator and denominator should be
319 % horizontally scaled. It should only be used if the real superior
320 % and inferior fonts are not available. For instance Stempel
321 % Garamond looks excellent if scaled 10\,\% extra horizontally, \emph{i.e.},
322 % by a factor of 1.1.}
323 % {1}
324 %
325 % \TemplateKey{v-scale}{real}
326 % {Same as \key{h-scale} only vertically. Probably not of much use
327 % but added for completeness.}
328 % {1}
329 %
330 % \TemplateKey{scale-factor}{real}
331 % {Fraction of the size of \key{slash-symbol}. Used for setting the
332 % font size of numerator and denominator. Usually a value of app.\
333 % \sfrac{5}{6} produces fine results. It should only be used if the
334 % real superior and inferior fonts are not available. As an example
335 % Stempel Garamond looks better if the factor is 0.9.}
336 % {0.83333}
337 %
338 % \TemplateKey{scale-relative}{choice}
339 % {If set to |true| the font size of the numerator and denominator
340 % is scaled with respect to the height of the \key{slash-symbol}. If
341 % set to |false| the font is scaled with respect to the total height
342 % of the \key{slash-symbol}.}
343 % {true}
344 %
345 % \TemplateKey{scaling}{choice}
346 % {If set to |true| the fonts are allowed to scale. If set to
347 % |false| they are not. See the `Janson' example for an application.}
348 % {true}
349 %
350 % \TemplateKey{numerator-top-sep}{length}
351 % {Dimension specifying the space between the top of the
352 % \key{slash-symbol} and the top of the numerator. If not specified,
353 % the depth of the solidus will be used, because this value will
354 % make the fraction look even.}
355 % {Unspecified}
356 %
357 % \TemplateKey{numerator-bot-sep}{length}
358 % {Dimension specifying the lift of the numerator from the
359 % baseline.}
360 % {Unspecified}
361 %
362 % \TemplateKey{denominator-bot-sep}{length}
363 % {Dimension specifying the lift of the denominator from the
364 % baseline.}
365 % {Unspecified}
366 %
367 % \TemplateKey{slash-right-kern}{length}
368 % {Dimension specifying the kerning between the \key{slash-symbol}
369 % and the numerator.}
370 % {\texttt{0pt}}
371 %
372 % \TemplateKey{slash-left-kern}{length}
373 % {Dimension specifying the kerning between the \key{slash-symbol}
374 % and the denominator.}
375 % {\texttt{0pt}}
376 %
377 % \TemplateKey{math-mode}{choice}
378 % {Are we in math mode or not?}
379 % {false}
380 %
381 % \TemplateKey{phantom}{tokenlist}
382 % {A character that suits the common cases. As we would mostly want
383 % to use numbers in text mode we choose a \enquote{tall} number, while in
384 % math it is somewhat different.}
385 % {8}
386 %
387 % \TemplateSemantics
388 %
389 % This template is also the foundation for the \enquote{math} template. The
390 % keys \key{slash-right-mkern} and \key{slash-left-mkern} can only
391 % be used in math mode and are not shown here.
392 %
393 % \end{TemplateDescription}
394 %
395 % \begin{TemplateDescription}{xfrac}{math}
396 %
397 % \TemplateKey{numerator-font}{tokenlist}
398 % {Font family specification to use for the numerator.}
399 % {\cs{number}\cs{fam}}
400 %
401 % \TemplateKey{slash-symbol}{tokenlist}
402 % {The separator symbol. If not specified the default value will be
403 % used instead.}
404 % {Virgule ($/$)}
405 %
406 % \TemplateKey{slash-symbol-font}{tokenlist}
407 % {Font family specification to use for the separator symbol.}
408 % {\cs{number}\cs{fam}}
409 %
410 % \TemplateKey{denominator-font}{tokenlist}
411 % {Font family specification to use for the denominator.}
412 % {\cs{number}\cs{fam}}
413 %
414 % \TemplateKey{scale-factor}{real}
415 % {Fraction of the size of \key{slash-symbol}. In math mode we
416 % cannot rely on the fonts to be able to scale, but giving a default
417 % scale of 0.7 fits into the regular size changing scheme---the
418 % default scheme has values $(D,T,S,SS)=(1,1,0.7,0.5)$ whereas we
419 % with a default \key{scale-factor} of 0.7 get $(1,1,0.7,0.49)$.
420 % That's close enough.}
421 % {0.7}
422 %
423 % \TemplateKey{scale-relative}{choice}
424 % {If set to |true| the font size of the numerator and denominator
425 % is scaled with respect to the height of the \key{slash-symbol}. If
426 % set to |false| the font is scaled with respect to the total height
427 % of the \key{slash-symbol}.}
428 % {false}
429 %
430 % \TemplateKey{scaling}{choice}
431 % {If set to |true| the fonts are allowed to scale. If set to
432 % |false| they are not. See the |plainmath| example for an application.}
433 % {true}
434 %
435 % \TemplateKey{numerator-top-sep}{length}
436 % {Dimension specifying the space between the top of the
437 % \key{slash-symbol} and the top of the numerator. If not specified,
438 % the depth of the virgule will be used, because this value will
439 % make the fraction look even.}
440 % {\texttt{0pt}}
441 %
442 % \TemplateKey{denominator-bot-sep}{length}
443 % {Dimension specifying the lift of the denominator from the
444 % baseline.}
445 % {\texttt{0pt}}
446 %
447 % \TemplateKey{slash-right-mkern}{muskip}
448 % {Same as \key{slash-right-kern} but for math mode only and should
449 % be specified in \texttt{mu} units.}
450 % {\texttt{-2mu}}
451 %
452 % \TemplateKey{slash-left-mkern}{muskip}
453 % {Same as \key{slash-left-kern} but for math mode only and should
454 % be specified in \texttt{mu} units.}
455 % {\texttt{-1mu}}
456 %
457 % \TemplateKey{math-mode}{choice}
458 % {Are we in math mode or not?}
459 % {true}
460 %
461 % \TemplateKey{phantom}{tokenlist}
462 % {A character that suits the common cases. In math we have a high
463 % risk of using a parenthesis, so we choose that. Text mode is
464 % another story.}
465 % {(^^A)
466 % }
467 %
468 % \TemplateSemantics
469 %
470 % This template is a restricted version of the |text| template. Only
471 % the keys that are different from the |text| template are shown
472 % here. Also bear in mind that the attributes \key{slash-left-kern}
473 % and \key{slash-right-kern} have no meaning in this template.
474 %
475 % \end{TemplateDescription}
476 %
477 % \end{documentation}
478 %
479 % \begin{implementation}
480 %
481 % \section{\pkg{xfrac} implementation}
482 %
483 % \begin{macrocode}
484 %<*package>
485 % \end{macrocode}
486 %
487 % \begin{macrocode}
488 %<@@=xfrac>
489 % \end{macrocode}
490 %
491 % Load \pkg{expl3} \enquote{up-front} only if required.
492 % \begin{macrocode}
493 \@ifundefined{ExplLoaderFileDate}
494 {\RequirePackage{expl3}}
495 {}
496 % \end{macrocode}
497 %
498 % Make sure that the version of \pkg{l3kernel} in use is sufficiently new.
499 % We use \cs{ExplFileDate} as \cs{@ifpackagelater} doesn't work for pre-loaded
500 % \pkg{expl3} in the absence of the package.
501 % \begin{macrocode}
502 \@ifl@t@r\ExplLoaderFileDate{2018-02-21}
503 {}
504 {%
505 \PackageError{xfrac}{Support package expl3 too old}
506 {%
507 You need to update your installation of 'l3kernel'.\MessageBreak
508 Loading~xfrac~will~abort!%
509 }%
510 \endinput
511 }%
512 % \end{macrocode}
513 %
514 % \begin{macrocode}
515 \ProvidesExplPackage{xfrac}{2024-06-04}{}
516 {Split-level fractions}
517 % \end{macrocode}
518 %
519 % \begin{macro}{\IfFormatAtLeastTF}
520 % Not present in older kernels: use the \LaTeXe{} mechanism as this is correct
521 % for this case.
522 % \begin{macrocode}
523 \providecommand \IfFormatAtLeastTF { \@ifl@t@r \fmtversion }
524 % \end{macrocode}
525 % \end{macro}
526 %
527 % \begin{macrocode}
528 \IfFormatAtLeastTF { 2020-02-02 }
529 { }
530 { \RequirePackage { textcomp } }
531 % \end{macrocode}
532 %
533 % \begin{macrocode}
534 \IfFormatAtLeastTF { 2020-10-01 }
535 { }
536 { \RequirePackage { xparse } }
537 % \end{macrocode}
538 %
539 % \begin{variable}{\l_@@_cm_std_bool}
540 % There is one option to support.
541 % \begin{macrocode}
542 \keys_define:nn { xfrac }
543 {
544 cm-recommended .choice:,
545 cm-recommended /
546 false .code:n =
547 { \bool_set_true:N \l_@@_cm_std_bool },
548 cm-recommended /
549 true .code:n =
550 { \bool_set_false:N \l_@@_cm_std_bool },
551 cm-recommended .default:n = { true },
552 cm-standard .bool_set:N = \l_@@_cm_std_bool
553 }
554 % \end{macrocode}
555 % \end{variable}
556 %
557 % \begin{macrocode}
558 \IfFormatAtLeastTF { 2022-06-01 }
559 { \ProcessKeyOptions }
560 {
561 \RequirePackage { l3keys2e }
562 \ProcessKeysOptions { xfrac }
563 }
564 % \end{macrocode}
565 %
566 % \begin{macrocode}
567 \IfFormatAtLeastTF { 2024-06-01 }
568 { }
569 { \RequirePackage { xtemplate } }
570 \cs_if_exist:NF \IfInstanceExistsTF
571 { \cs_new_eq:NN \IfInstanceExistsTF \IfInstanceExistTF }
572 % \end{macrocode}
573 %
574 % \begin{variable}{\l_@@_slash_box}
575 % \begin{variable}{\l_@@_tmp_box}
576 % In keeping with the \LaTeX3 philosophy, rather than use generic
577 % scratch boxes and get confused, \pkg{xfrac} reserves its own named
578 % working space.
579 % \begin{macrocode}
580 \box_new:N \l_@@_slash_box
581 \box_new:N \l_@@_tmp_box
582 % \end{macrocode}
583 % \end{variable}
584 % \end{variable}
585 %
586 % \begin{macro}{\@@_tmp:w}
587 % Used for the raised boxes: weird as it does not take an argument
588 % but the \cs{raisebox} does.
589 % \begin{macrocode}
590 \cs_new:Npn \@@_tmp:w { }
591 % \end{macrocode}
592 % \end{macro}
593 %
594 %
595 % \subsection{Initialisation of variables}
596 %
597 % Variables used in templates have to be set up: there is not
598 % much to say about these, other than that they must exist.
599 %
600 % \begin{variable}{\l_@@_denominator_bot_sep_dim}
601 % \begin{variable}{\l_@@_numerator_bot_sep_dim}
602 % \begin{variable}{\l_@@_numerator_top_sep_dim}
603 % \begin{variable}{\l_@@_slash_left_sep_dim}
604 % \begin{variable}{\l_@@_slash_right_sep_dim}
605 % Fixed lengths.
606 % \begin{macrocode}
607 \dim_new:N \l_@@_denominator_bot_sep_dim
608 \dim_new:N \l_@@_numerator_bot_sep_dim
609 \dim_new:N \l_@@_numerator_top_sep_dim
610 \dim_new:N \l_@@_slash_left_sep_dim
611 \dim_new:N \l_@@_slash_right_sep_dim
612 % \end{macrocode}
613 % \end{variable}
614 % \end{variable}
615 % \end{variable}
616 % \end{variable}
617 % \end{variable}
618 %
619 % \begin{variable}{\l_@@_slash_left_muskip}
620 % \begin{variable}{\l_@@_slash_right_muskip}
621 % Math mode skips.
622 % \begin{macrocode}
623 \muskip_new:N \l_@@_slash_left_muskip
624 \muskip_new:N \l_@@_slash_right_muskip
625 % \end{macrocode}
626 % \end{variable}
627 % \end{variable}
628 %
629 % \begin{variable}{\l_@@_hscale_fp}
630 % \begin{variable}{\l_@@_scale_factor_fp}
631 % \begin{variable}{\l_@@_vscale_fp}
632 % Floating point values.
633 % \begin{macrocode}
634 \fp_new:N \l_@@_hscale_fp
635 \fp_new:N \l_@@_scale_factor_fp
636 \fp_new:N \l_@@_vscale_fp
637 % \end{macrocode}
638 % \end{variable}
639 % \end{variable}
640 % \end{variable}
641 %
642 % \begin{variable}{\l_@@_denominator_font_tl}
643 % \begin{variable}{\l_@@_numerator_font_tl}
644 % \begin{variable}{\l_@@_phantom_tl }
645 % \begin{variable}{\l_@@_slash_symbol_tl}
646 % \begin{variable}{\l_@@_slash_symbol_font_tl}
647 % Token lists, which include floating-point numbers and math(s)
648 % skips.
649 % \begin{macrocode}
650 \tl_new:N \l_@@_denominator_font_tl
651 \tl_new:N \l_@@_numerator_font_tl
652 \tl_new:N \l_@@_phantom_tl
653 \tl_new:N \l_@@_slash_symbol_tl
654 \tl_new:N \l_@@_slash_symbol_font_tl
655 % \end{macrocode}
656 % \end{variable}
657 % \end{variable}
658 % \end{variable}
659 % \end{variable}
660 % \end{variable}
661 %
662 % \begin{macro}{\@@_fontscale:}
663 % \begin{macro}{\@@_math:n}
664 % \begin{macro}{\@@_denominator_font_change:}
665 % \begin{macro}{\@@_denominator_format:n}
666 % \begin{macro}{\@@_numerator_font_change:}
667 % \begin{macro}{\@@_numerator_format:n}
668 % \begin{macro}{\@@_relscale:}
669 % \begin{macro}{\@@_slash_symbol_font_change:}
670 % \begin{macro}{\@@_slash_symbol_format:n}
671 % \begin{macro}{\@@_text_or_math:n}
672 % Functions, either things which are calculated \enquote{on the fly}
673 % (no argument required) or are functions taking one argument in the
674 % code.
675 % \begin{macrocode}
676 \cs_new:Npn \@@_fontscale: { }
677 \cs_new:Npn \@@_math:n #1 { }
678 \cs_new:Npn \@@_denominator_font_change: { }
679 \cs_new:Npn \@@_denominator_format:n #1 { }
680 \cs_new:Npn \@@_numerator_font_change: { }
681 \cs_new:Npn \@@_numerator_format:n #1 { }
682 \cs_new:Npn \@@_relscale: { }
683 \cs_new:Npn \@@_slash_symbol_font_change: { }
684 \cs_new:Npn \@@_slash_symbol_format:n #1 { }
685 \cs_new:Npn \@@_text_or_math:n #1 { }
686 % \end{macrocode}
687 % \end{macro}
688 % \end{macro}
689 % \end{macro}
690 % \end{macro}
691 % \end{macro}
692 % \end{macro}
693 % \end{macro}
694 % \end{macro}
695 % \end{macro}
696 % \end{macro}
697 %
698 % \subsection{The template}
699 %
700 % There is only one object type in \pkg{xfrac}, rather unimaginatively
701 % named \texttt{xfrac}.
702 % \begin{macrocode}
703 \IfFormatAtLeastTF { 2024-06-01 }
704 { \NewTemplateType }
705 { \DeclareObjectType }
706 { xfrac } { 3 }
707 % \end{macrocode}
708 %
709 % A single template interface is used for both text and math(s), which
710 % does make a few things a little complex later.
711 % \begin{macrocode}
712 \DeclareTemplateInterface { xfrac } { text } { 3 }
713 {
714 denominator-bot-sep : length = \c_max_dim ,
715 denominator-font : tokenlist = \f@family ,
716 denominator-format : function 1 = #1 ,
717 h-scale : real = 1 ,
718 math-mode : choice { false , true }
719 = false ,
720 numerator-font : tokenlist = \f@family ,
721 numerator-format : function 1 = #1 ,
722 numerator-bot-sep : length = \c_max_dim ,
723 numerator-top-sep : length = \c_max_dim ,
724 phantom : tokenlist = 8 ,
725 scale-factor : real = 0.83333 ,
726 scale-relative : choice { false , true }
727 = true ,
728 scaling : choice { false , true }
729 = true ,
730 slash-left-kern : length = 0 pt ,
731 slash-left-mkern : muskip = -2 mu ,
732 slash-right-kern : length = 0 pt ,
733 slash-right-mkern : muskip = -1 mu ,
734 slash-symbol : tokenlist = \textfractionsolidus ,
735 slash-symbol-font : tokenlist = \f@family ,
736 slash-symbol-format : function 1 = #1 ,
737 v-scale : real = 1 ,
738 }
739 % \end{macrocode}
740 % Most of the variable binding is quite simple: of course, the choices
741 % are a little more complicated. That is particularly true where
742 % these have to set up \enquote{on the fly} functions.
743 % \begin{macrocode}
744 \DeclareTemplateCode { xfrac } { text } { 3 }
745 {
746 denominator-bot-sep = \l_@@_denominator_bot_sep_dim ,
747 denominator-font = \l_@@_denominator_font_tl ,
748 denominator-format = \@@_denominator_format:n ,
749 h-scale = \l_@@_hscale_fp ,
750 math-mode =
751 {
752 false = \cs_set_eq:NN \@@_math:n \use:n,
753 true = \cs_set_eq:NN \@@_math:n \ensuremath
754 },
755 numerator-font = \l_@@_numerator_font_tl ,
756 numerator-format = \@@_numerator_format:n ,
757 numerator-bot-sep = \l_@@_numerator_bot_sep_dim ,
758 numerator-top-sep = \l_@@_numerator_top_sep_dim ,
759 phantom = \l_@@_phantom_tl ,
760 scale-factor = \l_@@_scale_factor_fp ,
761 scale-relative =
762 {
763 false =
764 \cs_set:Npn \@@_relscale:
765 {
766 \dim_eval:n
767 { \box_ht:N \l_@@_tmp_box + \box_dp:N \l_@@_tmp_box }
768 },
769 true =
770 \cs_set:Npn \@@_relscale:
771 { \dim_eval:n { \box_ht:N \l_@@_slash_box } }
772 },
773 scaling =
774 {
775 false = \cs_set_eq:NN \@@_fontscale: \prg_do_nothing:,
776 true =
777 \cs_set:Npn \@@_fontscale:
778 {
779 \fontsize
780 { \fp_to_dim:n { \l_@@_scale_factor_fp * \@@_relscale: } }
781 { \c_zero_dim }
782 \selectfont
783 }
784 },
785 slash-left-kern = \l_@@_slash_left_sep_dim ,
786 slash-left-mkern = \l_@@_slash_left_muskip ,
787 slash-right-kern = \l_@@_slash_right_sep_dim ,
788 slash-right-mkern = \l_@@_slash_right_muskip ,
789 slash-symbol = \l_@@_slash_symbol_tl ,
790 slash-symbol-font = \l_@@_slash_symbol_font_tl ,
791 slash-symbol-format = \@@_slash_symbol_format:n ,
792 v-scale = \l_@@_vscale_fp
793 }
794 % \end{macrocode}
795 % The implementation part starts with applying all of the settings
796 % from above. The first part of the set up is then to determine
797 % whether the surroundings are text or math(s), and react accordingly.
798 % \begin{macrocode}
799 {
800 \mode_if_math:TF
801 {
802 \cs_set_eq:NN \@@_text_or_math:n \text
803 \cs_set:Npe \@@_denominator_font_change:
804 { \tex_fam:D \l_@@_denominator_font_tl }
805 \cs_set:Npe \@@_numerator_font_change:
806 { \tex_fam:D \l_@@_numerator_font_tl }
807 \cs_set:Npe \@@_slash_symbol_font_change:
808 { \tex_fam:D \l_@@_slash_symbol_font_tl }
809 }
810 {
811 \cs_set_eq:NN \@@_text_or_math:n \mbox
812 \cs_set:Npn \@@_denominator_font_change:
813 {
814 \fontfamily { \l_@@_denominator_font_tl }
815 \selectfont
816 }
817 \cs_set:Npn \@@_numerator_font_change:
818 {
819 \fontfamily { \l_@@_numerator_font_tl }
820 \selectfont
821 }
822 \cs_set:Npn \@@_slash_symbol_font_change:
823 {
824 \fontfamily { \l_@@_slash_symbol_font_tl }
825 \selectfont
826 }
827 }
828 % \end{macrocode}
829 % Everything is now either inside \cs{text} or an \cs{mbox}, depending
830 % upon the surroundings. First, there are some boxes to set up.
831 % \begin{macrocode}
832 \@@_text_or_math:n
833 {
834 \m@th
835 \hbox_set:Nn \l_@@_tmp_box
836 { \@@_math:n { \vphantom { ( ) } } }
837 \hbox_set:Nn \l_@@_slash_box
838 {
839 \@@_math:n
840 {
841 \@@_slash_symbol_format:n
842 {
843 \@@_math:n
844 {
845 \@@_slash_symbol_font_change:
846 \IfNoValueTF {#2}
847 { \l_@@_slash_symbol_tl } {#2}
848 }
849 }
850 }
851 }
852 % \end{macrocode}
853 % Check on the numerator separator dimensions. The code starts with the
854 % assumption that neither has been given, as this can then be used to
855 % set up a default, which is also used when both values are set
856 % erroneously.
857 % \begin{macrocode}
858 \cs_set:Npn \@@_tmp:w
859 {
860 \raisebox
861 {
862 \dim_eval:n
863 {
864 \box_ht:N \l_@@_slash_box
865 - \box_dp:N \l_@@_slash_box
866 - \height
867 }
868 }
869 }
870 \dim_compare:nNnTF
871 { \l_@@_numerator_top_sep_dim } = { \c_max_dim }
872 {
873 \dim_compare:nNnF
874 { \l_@@_numerator_bot_sep_dim } = { \c_max_dim }
875 {
876 \cs_set:Npn \@@_tmp:w
877 {
878 \raisebox
879 { \dim_use:N \l_@@_numerator_bot_sep_dim }
880 }
881 }
882 }
883 {
884 \dim_compare:nNnTF
885 { \l_@@_numerator_bot_sep_dim } = { \c_max_dim }
886 {
887 \cs_set:Npn \@@_tmp:w
888 {
889 \raisebox
890 {
891 \dim_eval:n
892 {
893 \box_ht:N \l_@@_slash_box
894 - \dim_use:N \l_@@_numerator_top_sep_dim
895 - \height
896 }
897 }
898 }
899 }
900 {
901 \msg_error:nn { xfrac }
902 { over-specified-numerator-sep }
903 }
904 }
905 % \end{macrocode}
906 % Typeset the numerator.
907 % \begin{macrocode}
908 \@@_tmp:w
909 {
910 \@@_fontscale:
911 \@@_numerator_format:n
912 {
913 \scalebox
914 { \fp_use:N \l_@@_hscale_fp }
915 [ \fp_use:N \l_@@_vscale_fp ]
916 {
917 \@@_math:n
918 {
919 \@@_numerator_font_change:
920 {
921 \vphantom { \l_@@_phantom_tl }
922 #1
923 }
924 }
925 }
926 }
927 }
928 \@@_math:n
929 { % THIS IS JUST WRONG!
930 \mode_if_math:TF
931 { \tex_mskip:D \l_@@_slash_right_muskip }
932 { \tex_hskip:D \l_@@_slash_right_sep_dim }
933 }
934 % \end{macrocode}
935 % Typeset the separator.
936 % \begin{macrocode}
937 \box_use:N \l_@@_slash_box
938 \@@_math:n
939 {
940 \mode_if_math:TF
941 { \tex_mskip:D \l_@@_slash_left_muskip }
942 { \tex_hskip:D \l_@@_slash_left_sep_dim }
943 }
944 % \end{macrocode}
945 % Typeset the denominator.
946 % \begin{macrocode}
947 \dim_compare:nNnTF
948 { \l_@@_denominator_bot_sep_dim } = { \c_max_dim }
949 {
950 \cs_set:Npn \@@_tmp:w
951 { \raisebox { - \box_dp:N \l_@@_slash_box } }
952 }
953 {
954 \cs_set:Npn \@@_tmp:w
955 {
956 \raisebox
957 { \dim_use:N \l_@@_denominator_bot_sep_dim }
958 }
959 }
960 \@@_tmp:w
961 {
962 \@@_fontscale:
963 \@@_denominator_format:n
964 {
965 \scalebox
966 { \fp_use:N \l_@@_hscale_fp }
967 [ \fp_use:N \l_@@_vscale_fp ]
968 {
969 \@@_math:n
970 {
971 \@@_denominator_font_change:
972 {
973 \vphantom { \l_@@_phantom_tl }
974 #3
975 }
976 }
977 }
978 }
979 }
980 }
981 }
982 % \end{macrocode}
983 %
984 % Since math(s) and text mode are wildly different entities we define a
985 % separate template for each. You already saw the \enquote{text}
986 % template, and here is the \enquote{math} template.
987 % \begin{macrocode}
988 \IfFormatAtLeastTF { 2024-06-01 }
989 {
990 \DeclareTemplateCopy { xfrac } { math } { text }
991 \EditTemplateDefaults { xfrac } { math }
992 }
993 { \DeclareRestrictedTemplate { xfrac } { text } { math } }
994 {
995 numerator-font = \number \fam ,
996 slash-symbol = / ,
997 slash-symbol-font = \number \fam ,
998 denominator-font = \number \fam ,
999 scale-factor = 0.7 ,
1000 scale-relative = false ,
1001 scaling = true ,
1002 denominator-bot-sep = 0 pt ,
1003 math-mode = true ,
1004 phantom = ( % )
1005 }
1006 % \end{macrocode}
1007 %
1008 %\subsection{The standard instances}
1009 %
1010 % For the default instances we just use the relevant templates with
1011 % the default settings.
1012 %
1013 % The default \enquote{text} instance.
1014 % \begin{macrocode}
1015 \DeclareInstance { xfrac } { default } { text } { }
1016 % \end{macrocode}
1017 %
1018 % The default \enquote{math(s)} instance. We annot set the
1019 % |numerator-top-sep| in the restricted template above.
1020 % \begin{macrocode}
1021 \DeclareInstance { xfrac } { mathdefault } { math }
1022 { numerator-top-sep = 0pt }
1023 % \end{macrocode}
1024 %
1025 % Default Computer Modern setup. Far from optimal, but better than
1026 % nothing.
1027 % \begin{macrocode}
1028 \DeclareInstance { xfrac } { cmr } { text }
1029 {
1030 denominator-bot-sep = 0 pt ,
1031 numerator-top-sep = 0.2 ex ,
1032 slash-left-kern = -0.1 em ,
1033 slash-right-kern = -0.1 em
1034 }
1035 \DeclareInstance { xfrac } { cmss } { text }
1036 {
1037 denominator-bot-sep = 0 pt ,
1038 numerator-top-sep = 0.2 ex ,
1039 slash-left-kern = -0.1 em ,
1040 slash-right-kern = -0.1 em
1041 }
1042 \DeclareInstance { xfrac } { cmtt } { text }
1043 {
1044 denominator-bot-sep = 0 pt ,
1045 numerator-top-sep = 0.2 ex ,
1046 slash-left-kern = -0.1 em ,
1047 slash-right-kern = -0.1 em
1048 }
1049 % \end{macrocode}
1050 %
1051 % We can do better for the Computer Modern fonts. For cmr and cmss
1052 % we choose Times, and for cmtt use Palatino.
1053 % \begin{macrocode}
1054 \bool_if:NF \l_@@_cm_std_bool
1055 {
1056 \DeclareInstance { xfrac } { cmr } { text }
1057 { slash-symbol-font = ptm }
1058 \DeclareInstance { xfrac } { cmss } { text }
1059 { slash-symbol-font = ptm }
1060 \DeclareInstance { xfrac } { cmtt } { text }
1061 { slash-symbol-font = ppl }
1062 }
1063 % \end{macrocode}
1064 %
1065 % Things works slightly better with Latin Modern.
1066 % \begin{macrocode}
1067 \DeclareInstance { xfrac } { lmr } { text }
1068 {
1069 denominator-bot-sep = 0 pt ,
1070 numerator-top-sep = 0.1 ex ,
1071 slash-left-kern = -0.15 em ,
1072 slash-right-kern = -0.15 em
1073 }
1074 \DeclareInstance { xfrac } { lmss } { text }
1075 {
1076 denominator-bot-sep = 0 pt ,
1077 numerator-top-sep = 0 pt ,
1078 slash-left-kern = -0.15 em ,
1079 slash-right-kern = -0.15 em
1080 }
1081 \DeclareInstance { xfrac } { lmtt } { text }
1082 {
1083 denominator-bot-sep = 0 pt ,
1084 numerator-top-sep = 0 pt ,
1085 slash-left-kern = -0.15 em ,
1086 slash-right-kern = -0.15 em
1087 }
1088 % \end{macrocode}
1089 %
1090 % \subsection{The user command}
1091 %
1092 % \begin{macro}{\sfrac}
1093 % Currently there is just a single user command. \cs{sfrac} takes
1094 % two mandatory arguments: numerator and denominator. It can take an
1095 % optional argument between the mandatory specifying the separator
1096 % like this:
1097 % \begin{verbatim}
1098 % \sfrac{7}[/]{12}
1099 % \end{verbatim}
1100 % It also has an optional argument that comes before the first
1101 % mandatory argument. If used it will use that instance instead of
1102 % the auto-detected one, so a user who has defined the instance
1103 % \enquote{cmr2} may use
1104 % \begin{verbatim}
1105 % \sfrac[cmr2]{7}{12}
1106 % \end{verbatim}
1107 % and get the settings from \enquote{cmr2} instead of the settings of
1108 % the current font family.
1109 % \begin{macrocode}
1110 \NewDocumentCommand \sfrac { o m o m }
1111 {
1112 \mode_if_math:TF
1113 {
1114 \IfInstanceExistsTF { xfrac } { mathfam \number \fam }
1115 { \UseInstance { xfrac } { mathfam \number \fam } }
1116 { \UseInstance { xfrac } { mathdefault } }
1117 {#2} {#3} {#4}
1118 }
1119 {
1120 \IfInstanceExistsTF { xfrac } {#1}
1121 { \UseInstance { xfrac } {#1} }
1122 {
1123 \IfInstanceExistsTF { xfrac } { \f@family }
1124 { \UseInstance { xfrac } { \f@family } }
1125 { \UseInstance { xfrac } { default } }
1126 }
1127 {#2} {#3} {#4}
1128 }
1129 }
1130 % \end{macrocode}
1131 % \end{macro}
1132 %
1133 % \subsection{Messages}
1134 %
1135 % Just the one.
1136 % \begin{macrocode}
1137 \msg_new:nnnn { xfrac } { over-specified-numerator-sep }
1138 { You~have~specified~both~"numerator-top-sep"~and~"numerator-bot-sep". }
1139 { I~will~pretend~that~you~didn't~specify~either~of~them. }
1140 % \end{macrocode}
1141 %
1142 % \begin{macrocode}
1143 %</package>
1144 % \end{macrocode}
1145 %
1146 % \end{implementation}
1147 %
1148 % \PrintIndex