| 1 |
% \iffalse meta-comment |
| 2 |
% |
| 3 |
%% File: latex-lab-context.dtx (C) Copyright 2025 LaTeX Project |
| 4 |
% |
| 5 |
% It may be distributed and/or modified under the conditions of the |
| 6 |
% LaTeX Project Public License (LPPL), either version 1.3c of this |
| 7 |
% license or (at your option) any later version. The latest version |
| 8 |
% of this license is in the file |
| 9 |
% |
| 10 |
% https://www.latex-project.org/lppl.txt |
| 11 |
% |
| 12 |
% |
| 13 |
% The development version of the bundle can be found below |
| 14 |
% |
| 15 |
% https://github.com/latex3/latex2e/required/latex-lab |
| 16 |
% |
| 17 |
% for those people who are interested or want to report an issue. |
| 18 |
% |
| 19 |
\def\ltlabcontextdate{2025-10-09} |
| 20 |
\def\ltlabcontextversion{0.5b} |
| 21 |
%<*driver> |
| 22 |
\DocumentMetadata{tagging=on,pdfstandard=ua-2,lang=en} |
| 23 |
\documentclass{l3doc} |
| 24 |
\usepackage{latex-lab-testphase-l3doc} |
| 25 |
\EnableCrossrefs |
| 26 |
\CodelineIndex |
| 27 |
\begin{document} |
| 28 |
\DocInput{latex-lab-context.dtx} |
| 29 |
\end{document} |
| 30 |
%</driver> |
| 31 |
% |
| 32 |
% \fi |
| 33 |
% |
| 34 |
% \title{The \textsf{latex-lab-context} package\\ |
| 35 |
% Providing context for template instances and code that needs |
| 36 |
% to know where and when it is executed} |
| 37 |
% |
| 38 |
% \author{\LaTeX{} Project\thanks{Initial implementation done by Frank Mittelbach}} |
| 39 |
% \date{v\ltlabcontextversion\ \ltlabcontextdate} |
| 40 |
% |
| 41 |
% \maketitle |
| 42 |
% |
| 43 |
% \newcommand{\xt}[1]{\textsl{\textsf{#1}}} |
| 44 |
% \newcommand{\TODO}[1]{\textbf{[TODO:} #1\textbf{]}} |
| 45 |
% |
| 46 |
% \providecommand\hook[1]{\texttt{#1}} |
| 47 |
% |
| 48 |
% \begin{abstract} |
| 49 |
% \end{abstract} |
| 50 |
% |
| 51 |
% \section{Introduction} |
| 52 |
% |
| 53 |
% In this module we implement the concept of \enquote{contexts} |
| 54 |
% within the document and depending on the current context |
| 55 |
% formatting behavior might change. The main application for this |
| 56 |
% is in instances of templates (where things can be easily automated |
| 57 |
% to react to context), but it is probably also applicable to other |
| 58 |
% situations (but these might need more thought). |
| 59 |
% |
| 60 |
% \subsection{Definition of a \enquote{context}} |
| 61 |
% |
| 62 |
% The \enquote{context} is an attribute of every point of the |
| 63 |
% formatted document, i.e., at each point during the formatting one |
| 64 |
% can determine in which \enquote{context} the formatting happens |
| 65 |
% and based on this adjust the formatting method. |
| 66 |
% |
| 67 |
% The \enquote{context} is not an entirely flat structure: we |
| 68 |
% distinguish between the \enquote{primary context} and the |
| 69 |
% \enquote{secondary context} both of which can be changed |
| 70 |
% individually based on a number of rules as discussed below. |
| 71 |
% |
| 72 |
% Any context is denoted by a name (\verb=[a-z]*= letters only). The |
| 73 |
% empty name is allowed to ease specification of the common case |
| 74 |
% (i.e., the default \enquote{primary context} is the main galley |
| 75 |
% and by default no \enquote{secondary context} is specified). |
| 76 |
% |
| 77 |
% \subsection{The \enquote{primary context}} |
| 78 |
% |
| 79 |
% The \enquote{primary context} is described with a fixed (but |
| 80 |
% extensible?) set of names: |
| 81 |
% \begin{itemize} |
| 82 |
% |
| 83 |
% \item \meta{empty} denotes that we are in \enquote{galley} context |
| 84 |
% producing text for the page; |
| 85 |
% |
| 86 |
% \item \texttt{caption} |
| 87 |
% denotes that we are typesetting the caption text of a \cs{caption} command |
| 88 |
% --- this is also used for captions outside of floats, |
| 89 |
% |
| 90 |
% \item \texttt{footnote} |
| 91 |
% denotes that we are typesetting the footnote text; |
| 92 |
% |
| 93 |
% \item \texttt{float} |
| 94 |
% denotes that we are typesetting a float; |
| 95 |
% |
| 96 |
% \item \texttt{marginal} |
| 97 |
% denotes that we are typesetting a marginal; |
| 98 |
% |
| 99 |
% \item \texttt{header} |
| 100 |
% denotes that we are typesetting the running header; |
| 101 |
% |
| 102 |
% \item \texttt{footer} |
| 103 |
% denotes that we are typesetting the running footer. |
| 104 |
% |
| 105 |
% \end{itemize} |
| 106 |
% |
| 107 |
% When the \enquote{primary context} is set it replaces the current |
| 108 |
% \enquote{primary context} and resets the \enquote{secondary |
| 109 |
% context} to \meta{empty}. The setting is local, i.e., it obeys |
| 110 |
% grouping. |
| 111 |
% |
| 112 |
% It would be possible to be more granular, i.e., differentiate |
| 113 |
% between types of float etc. But for now I suggest to start out |
| 114 |
% with this small set. |
| 115 |
% |
| 116 |
% We may want to require that a context name is declared before its |
| 117 |
% first use, e.g., via \cs{DeclarePrimaryContext} (right now this is |
| 118 |
% not necessary. |
| 119 |
% |
| 120 |
% |
| 121 |
% \subsection{The \enquote{secondary context}} |
| 122 |
% |
| 123 |
% The \enquote{secondary context} concerns itself (for now) only |
| 124 |
% with font size changes, because that is most commonly a cause for |
| 125 |
% layout changes. It is described through a fixed (but extensible?) |
| 126 |
% set of names: |
| 127 |
% |
| 128 |
% \begin{itemize} |
| 129 |
% \item |
| 130 |
% \meta{empty} denotes that there is no special secondary |
| 131 |
% context, i.e., that we are producing material in \cs{normalsize}; |
| 132 |
% |
| 133 |
% \item \texttt{tiny} |
| 134 |
% denotes that we are typesetting in \cs{tiny} font size; |
| 135 |
% |
| 136 |
% \item and similarly for \texttt{scriptsize}, |
| 137 |
% \texttt{footnotesize}, \texttt{small}, \texttt{large}, |
| 138 |
% \texttt{Large}, \texttt{LARGE}, \texttt{huge}, and \texttt{Huge}. |
| 139 |
% \end{itemize} |
| 140 |
% The above list of secondary contexts are automatically set by the |
| 141 |
% standard \LaTeX{} font size commands (as part of \cs{@setfontsize}). Classes |
| 142 |
% that use additional font size commands but do not use the |
| 143 |
% \LaTeXe{} command for this need to explicitly set the secondary |
| 144 |
% context with \cs{SetSecondaryContext} if they want their size be |
| 145 |
% recognized as a context. |
| 146 |
% |
| 147 |
% |
| 148 |
% |
| 149 |
% \section{Setting context} |
| 150 |
% |
| 151 |
% \begin{function}{\SetPrimaryContext} |
| 152 |
% \begin{syntax} |
| 153 |
% \cs{SetPrimaryContext} \Arg{new context} |
| 154 |
% \end{syntax} |
| 155 |
% The \meta{new context} should be a name consisting only of |
| 156 |
% lowercase letters a--z (and to make sense only a name that is |
| 157 |
% actually used---typos will go undetected). |
| 158 |
% |
| 159 |
% This declaration checks if there is a rule for the combination of |
| 160 |
% the current context and the requested \meta{new context} (see |
| 161 |
% \cs{DeclarePrimaryContextRule}) and if so applies it to determine |
| 162 |
% to which context the \enquote{primary context} should be set. |
| 163 |
% If there is no such rule then \meta{new context} will be used |
| 164 |
% unconditionally. |
| 165 |
% |
| 166 |
% As a second action the command executes |
| 167 |
% \cs{SetSecondaryContext}\texttt{\{\}}, i.e., it clears the |
| 168 |
% \enquote{secondary context}. |
| 169 |
% |
| 170 |
% The effects are local, i.e., both context changes revert to the |
| 171 |
% previous value at the end of the current group. |
| 172 |
% \end{function} |
| 173 |
% |
| 174 |
% |
| 175 |
% |
| 176 |
% \begin{function}{\DeclarePrimaryContextRule} |
| 177 |
% \begin{syntax} |
| 178 |
% \cs{DeclarePrimaryContextRule} \Arg{current}\Arg{new}\Arg{result} |
| 179 |
% \end{syntax} |
| 180 |
% This declaration defines the rule that when the current primary |
| 181 |
% context is \meta{current} and \meta{new} is requested as the new |
| 182 |
% context then instead \meta{result} is made the new |
| 183 |
% \enquote{primary context}. |
| 184 |
% |
| 185 |
% If \texttt{*} is used in the first argument then this indicates |
| 186 |
% any current context and thus \meta{new} is always replaced by |
| 187 |
% \meta{result}. This can be more concisely written as |
| 188 |
% \begin{quote} |
| 189 |
% \cs{DeclarePrimaryContextRule*} \Arg{new}\Arg{result} |
| 190 |
% \end{quote} |
| 191 |
% if preferred. |
| 192 |
% |
| 193 |
% Declaring such rules is a global operation. |
| 194 |
% \end{function} |
| 195 |
% |
| 196 |
% |
| 197 |
% |
| 198 |
% \begin{function}{\SetSecondaryContext} |
| 199 |
% \begin{syntax} |
| 200 |
% \cs{SetSecondaryContext} \Arg{new context} |
| 201 |
% \end{syntax} |
| 202 |
% The \meta{new context} should be a name consisting only of |
| 203 |
% lowercase letters a--z (and to make sense only a name that is |
| 204 |
% actually used---typos will go undetected). |
| 205 |
% |
| 206 |
% This declaration checks if there is a rule for the combination of |
| 207 |
% the current secondary context and the requested \meta{new context} |
| 208 |
% (see \cs{DeclareSecondaryContextRule}) and if so applies it to |
| 209 |
% determine to which context the \enquote{secondary context} should |
| 210 |
% be set. If there is no such rule then \meta{new context} will |
| 211 |
% be used unconditionally. |
| 212 |
% |
| 213 |
% The effect is local, i.e., the context change reverts to the |
| 214 |
% previous value at the end of the current group. |
| 215 |
% \end{function} |
| 216 |
% |
| 217 |
% |
| 218 |
% |
| 219 |
% \begin{function}{\DeclareSecondaryContextRule} |
| 220 |
% \begin{syntax} |
| 221 |
% \cs{DeclareSecondaryContextRule} \Arg{current}\Arg{new}\Arg{result} |
| 222 |
% \end{syntax} |
| 223 |
% This declaration defines the rule that when the current secondary |
| 224 |
% context is \meta{current} and \meta{new} is requested as the new |
| 225 |
% context then instead \meta{result} is made the new |
| 226 |
% \enquote{secondary context}. |
| 227 |
% |
| 228 |
% If \texttt{*} is used in the first argument then this indicates |
| 229 |
% any current context and thus \meta{new} is always replaced by |
| 230 |
% \meta{result}. This can be more concisely written as |
| 231 |
% \begin{quote} |
| 232 |
% \cs{DeclareSecondaryContextRule*} \Arg{new}\Arg{result} |
| 233 |
% \end{quote} |
| 234 |
% if preferred. |
| 235 |
% |
| 236 |
% Declaring such rules is a global operation. |
| 237 |
% \end{function} |
| 238 |
% |
| 239 |
% |
| 240 |
% |
| 241 |
% \section{Context usage in template instances} |
| 242 |
% |
| 243 |
% If a template instance is used via |
| 244 |
% \cs{UseInstance}\Arg{type}\Arg{inst-name} then this normally |
| 245 |
% results in calling up an instance of type \meta{type} with the name |
| 246 |
% \meta{inst-name}.\footnote{Such instances are defined with |
| 247 |
% \cs{DeclareInstance} or \cs{DeclareInstanceCopy}, see the |
| 248 |
% documentation in |
| 249 |
% \texttt{lttemplates-doc.pdf}.} |
| 250 |
% |
| 251 |
% However, when the \enquote{primary context} and/or the \enquote{secondary |
| 252 |
% context} is non-empty then \cs{UseInstance} searches for an |
| 253 |
% instance that is especially tailored to the current context. |
| 254 |
% This works as follows: |
| 255 |
% \begin{itemize} |
| 256 |
% \item The string\texttt{:}\meta{primary |
| 257 |
% context}\texttt{:}\meta{secondary context} is appended to |
| 258 |
% \meta{inst-name} and if that instance exist it is |
| 259 |
% used.\footnote{Note that this means that |
| 260 |
% if the \meta{primary context} is empty we effectively append |
| 261 |
% \texttt{::}\meta{secondary context}.} |
| 262 |
% \item |
| 263 |
% If not then \meta{inst-name}\texttt{:}\meta{primary context} is |
| 264 |
% tried next. |
| 265 |
% \item |
| 266 |
% If that doesn't exist either then \meta{inst-name} is used as usual. |
| 267 |
% \end{itemize} |
| 268 |
% |
| 269 |
% This means it becomes trivial to alter the behavior of |
| 270 |
% instances if they appear in a special typesetting context. For |
| 271 |
% example, in \LaTeXe{} display blocks, e.g., lists, center, etc., all |
| 272 |
% changed their vertical spacing setup if the surrounding text was |
| 273 |
% in \cs{small} or in \cs{footnotesize} (but did nothing if you |
| 274 |
% typeset in, say, \cs{Large} which had the strange effect that |
| 275 |
% lists in \cs{Large} or \cs{huge} got whatever was set by a size |
| 276 |
% command that changed list parameters). |
| 277 |
% |
| 278 |
% With the new context model all you have to do is to provide |
| 279 |
% additional instances, e.g., if \texttt{itemize-1} is the instance |
| 280 |
% name for itemized lists on the first level then |
| 281 |
% \texttt{itemize-1:footnote} would be the instance to be used if an |
| 282 |
% itemize environment is used within a footnote. |
| 283 |
% |
| 284 |
% In addition (or alternatively) you could setup |
| 285 |
% \texttt{itemize-1::Large} which would be selected if the |
| 286 |
% itemize is in the main galley (empty primary context) and the |
| 287 |
% fontsize at the outside is \cs{Large}. |
| 288 |
% |
| 289 |
% |
| 290 |
% \section{Notes} |
| 291 |
% |
| 292 |
% With special classes, e.g., \texttt{ltx-talk}, additional primaries |
| 293 |
% could be added (and secondaries could be using the modes rather |
| 294 |
% than or in addition to the fontsizes). Or the modes could be part |
| 295 |
% of the primary names \dots. Could do with some experimentation |
| 296 |
% what works best. |
| 297 |
% |
| 298 |
% If the design of a class requires identical behavior in different |
| 299 |
% contexts, e.g., the same behavior in \texttt{header} and |
| 300 |
% \texttt{footer}, it is possible to simplify the setup by only |
| 301 |
% specifying the design for the \texttt{header} context and then |
| 302 |
% declaring: |
| 303 |
%\begin{verbatim} |
| 304 |
% \DeclarePrimaryContextRule{*}{footer}{header} |
| 305 |
%\end{verbatim} |
| 306 |
% |
| 307 |
% One can also add new primary or secondary contexts simply by making |
| 308 |
% use of \cs{Set...Context} declarations with a new name and then |
| 309 |
% use these named contexts when setting up special instance |
| 310 |
% versions. |
| 311 |
% |
| 312 |
% What is currently not so easy is to get rid of context names that |
| 313 |
% have been set up, e.g., to use your own \texttt{wipple} and |
| 314 |
% \texttt{wopple} as secondary contexts and make sure that fontsize |
| 315 |
% commands to not overwrite the context. Right now that would |
| 316 |
% require a lot of rules like |
| 317 |
%\begin{verbatim} |
| 318 |
% \DeclareSecondaryContextRule{wipple}{tiny}{wipple} |
| 319 |
% \DeclareSecondaryContextRule{wipple}{footnotesize}{wipple} |
| 320 |
% \DeclareSecondaryContextRule{wipple}{scriptsize}{wipple} |
| 321 |
% ... |
| 322 |
%\end{verbatim} |
| 323 |
% so perhaps this interface needs changes or some augmentation, if |
| 324 |
% such thing turn out to be useful, e.g., supporting |
| 325 |
% \verb=\DeclareSecondaryContextRule{*}{tiny}{*}= to indicate this. |
| 326 |
% |
| 327 |
% Another possibility is to support several independent dimensions |
| 328 |
% as secondary contexts (then \enquote{fontsize} could be one and |
| 329 |
% the \enquote{wipple-wopple} one the other. which of these are used |
| 330 |
% when instances are selected could then (perhaps) be a function of |
| 331 |
% the template type but for a heading type looking at |
| 332 |
% front/main/back matter would be more appropriate.\footnote{Then, of |
| 333 |
% course, a primary context change would not reset any secondary |
| 334 |
% context.} |
| 335 |
% |
| 336 |
% Bottom line, what is here is nothing more than a first prototype |
| 337 |
% and likely to change to some extent. |
| 338 |
% |
| 339 |
% \section{Currently mainly internal interfaces} |
| 340 |
% |
| 341 |
% \begin{variable}{\l_context_primary_str} |
| 342 |
% This variable holds the current primary context name (or is empty |
| 343 |
% if we are in the context of the main galley). |
| 344 |
% \end{variable} |
| 345 |
% |
| 346 |
% \begin{variable}{\l_context_secondary_str} |
| 347 |
% This variable holds the current secondary context name (or is |
| 348 |
% empty if we are typesetting in \cs{normalsize}). It is by default |
| 349 |
% automatically set by all fontsize commands in core \LaTeX{}. If |
| 350 |
% additional secondary contexts are used then the variable may not |
| 351 |
% be empty even if we are typesetting in \cs{normalsize} |
| 352 |
% \end{variable} |
| 353 |
|
| 354 |
% \section{Debugging} |
| 355 |
% |
| 356 |
% \begin{function}{\DebugContextsOn,\DebugContextsOff, \context_debug_on:, \context_debug_off:} |
| 357 |
% |
| 358 |
% These commands enable/disable debugging messages for context related |
| 359 |
% processing. |
| 360 |
% |
| 361 |
% \end{function} |
| 362 |
% |
| 363 |
% \begin{function}{\DebugTemplatesOn,\DebugTemplatesOff, \template_debug_on:, \template_debug_off:} |
| 364 |
% |
| 365 |
% These commands enable/disable debugging messages for template related |
| 366 |
% processing. (Belongs into \file{lttemplates.dtx} one day.) |
| 367 |
% |
| 368 |
% \end{function} |
| 369 |
% |
| 370 |
% |
| 371 |
% \section{Changes to internals of \LaTeX} |
| 372 |
% |
| 373 |
% \begin{function}{\@setfontsize} |
| 374 |
% This command has be augmented to execute \cs{SetSecondaryContext} |
| 375 |
% with the name of the current fontsize command as the context argument. |
| 376 |
% \end{function} |
| 377 |
% |
| 378 |
% \begin{function}{\@float} |
| 379 |
% This command has been augmented to execute \cs{SetPrimaryContext}\texttt{\{float\}}. |
| 380 |
% This results in the body of all real floats being typeset in the |
| 381 |
% primary \texttt{float} context. Not covered are packages or |
| 382 |
% methods that make floats not using \cs{@float} or making pseudo |
| 383 |
% floats, e.g., something not floating but having a caption. |
| 384 |
% \end{function} |
| 385 |
% |
| 386 |
% |
| 387 |
% |
| 388 |
% |
| 389 |
% |
| 390 |
% \section{Implementation} |
| 391 |
% \begin{macrocode} |
| 392 |
%<*package> |
| 393 |
%<@@=tag> |
| 394 |
% \end{macrocode} |
| 395 |
% \begin{macrocode} |
| 396 |
\ProvidesExplPackage {latex-lab-testphase-context} |
| 397 |
{\ltlabcontextdate} |
| 398 |
{\ltlabcontextversion} |
| 399 |
{Providing context for instance, etc.} |
| 400 |
% \end{macrocode} |
| 401 |
% |
| 402 |
% \begin{macrocode} |
| 403 |
%<*package> |
| 404 |
%<@@=context> |
| 405 |
% \end{macrocode} |
| 406 |
% |
| 407 |
% \begin{macrocode} |
| 408 |
\RequirePackage{latex-lab-testphase-block} |
| 409 |
% \end{macrocode} |
| 410 |
% |
| 411 |
% |
| 412 |
% |
| 413 |
% |
| 414 |
% |
| 415 |
% \subsubsection{Debugging} |
| 416 |
% |
| 417 |
% |
| 418 |
% \begin{variable}{\g_@@_debug_bool} |
| 419 |
% |
| 420 |
% \begin{macrocode} |
| 421 |
\bool_new:N \g_@@_debug_bool |
| 422 |
% \end{macrocode} |
| 423 |
% \end{variable} |
| 424 |
% |
| 425 |
% |
| 426 |
% \begin{macro}{\@@_debug:n,\@@_debug_typeout:n} |
| 427 |
% |
| 428 |
% \begin{macrocode} |
| 429 |
\cs_new_eq:NN \@@_debug:n \use_none:n |
| 430 |
\cs_new_eq:NN \@@_debug_typeout:n \use_none:n |
| 431 |
% \end{macrocode} |
| 432 |
% \end{macro} |
| 433 |
% |
| 434 |
% \begin{macro}{\context_debug_on:,\context_debug_off:, |
| 435 |
% \@@_debug_gset:} |
| 436 |
% \begin{macrocode} |
| 437 |
\cs_new_protected:Npn \context_debug_on: |
| 438 |
{ |
| 439 |
\bool_gset_true:N \g_@@_debug_bool |
| 440 |
\@@_debug_gset: |
| 441 |
} |
| 442 |
% \end{macrocode} |
| 443 |
% |
| 444 |
% \begin{macrocode} |
| 445 |
\cs_new_protected:Npn \context_debug_off: |
| 446 |
{ |
| 447 |
\bool_gset_false:N \g_@@_debug_bool |
| 448 |
\@@_debug_gset: |
| 449 |
} |
| 450 |
% \end{macrocode} |
| 451 |
% |
| 452 |
% \begin{macrocode} |
| 453 |
\cs_new_protected:Npn \@@_debug_gset: |
| 454 |
{ |
| 455 |
\cs_gset_protected:Npx \@@_debug:n ##1 |
| 456 |
{ \bool_if:NT \g_@@_debug_bool {##1} } |
| 457 |
\cs_gset_protected:Npx \@@_debug_typeout:n ##1 |
| 458 |
{ \bool_if:NT \g_@@_debug_bool { \typeout{[Context]~ ==>~ ##1} } } |
| 459 |
} |
| 460 |
% \end{macrocode} |
| 461 |
% \end{macro} |
| 462 |
% |
| 463 |
% |
| 464 |
% \begin{macro}{\DebugContextsOn,\DebugContextsOff} |
| 465 |
% |
| 466 |
% \begin{macrocode} |
| 467 |
\cs_new_protected:Npn \DebugContextsOn { \context_debug_on: } |
| 468 |
\cs_new_protected:Npn \DebugContextsOff { \context_debug_off: } |
| 469 |
% \end{macrocode} |
| 470 |
% |
| 471 |
% \begin{macrocode} |
| 472 |
\DebugContextsOff |
| 473 |
% \end{macrocode} |
| 474 |
% \end{macro} |
| 475 |
% |
| 476 |
% |
| 477 |
% |
| 478 |
% |
| 479 |
% \begin{macro}{\l_context_primary_str} |
| 480 |
% Variable to hold the name of the current primary context. |
| 481 |
% \begin{macrocode} |
| 482 |
\str_new:N \l_context_primary_str |
| 483 |
% \end{macrocode} |
| 484 |
% \end{macro} |
| 485 |
% |
| 486 |
% |
| 487 |
% |
| 488 |
% |
| 489 |
% \begin{macro}{\SetPrimaryContext} |
| 490 |
% Change the context: if the target context is empty, just do |
| 491 |
% it. Otherwise if we have a rule for the current context and new context |
| 492 |
% use that; otherwise if there is a star rule apply that; otherwise |
| 493 |
% set the new value as requested, |
| 494 |
% \begin{macrocode} |
| 495 |
\cs_new_protected:Npn \SetPrimaryContext #1 { |
| 496 |
\str_set:Ne \l_context_primary_str |
| 497 |
{ \tl_if_empty:nF { #1 } |
| 498 |
{ \cs_if_exist_use:cF |
| 499 |
{ g_@@_primary_ \l_context_primary_str _ #1 _tl } |
| 500 |
{ \cs_if_exist_use:cF |
| 501 |
{ g_@@_primary_ * _ #1 _tl } |
| 502 |
{ #1 } |
| 503 |
} |
| 504 |
} |
| 505 |
} |
| 506 |
\@@_debug_typeout:n{set~primary~ <-~ \l_context_primary_str } |
| 507 |
% \end{macrocode} |
| 508 |
% Also reset the secondary context. |
| 509 |
% \begin{macrocode} |
| 510 |
\SetSecondaryContext {} |
| 511 |
} |
| 512 |
% \end{macrocode} |
| 513 |
% \end{macro} |
| 514 |
% |
| 515 |
% |
| 516 |
% \begin{macro}{\DeclarePrimaryContextRule} |
| 517 |
% Rules are simply stored in macro names: |
| 518 |
% \begin{macrocode} |
| 519 |
\cs_new_protected:Npn \DeclarePrimaryContextRule #1#2#3 { |
| 520 |
\tl_gclear_new:c { g_@@_primary_ #1 _ #2 _tl } |
| 521 |
\tl_gset:cn { g_@@_primary_ #1 _ #2 _tl } {#3} |
| 522 |
} |
| 523 |
% \end{macrocode} |
| 524 |
% \end{macro} |
| 525 |
% |
| 526 |
% |
| 527 |
% \begin{macro}{\l_context_secondary_str} |
| 528 |
% Variable to hold the name of the current secondary context. |
| 529 |
% \TODO{perhaps to be replaced by a multi-dimensional solution} |
| 530 |
% \begin{macrocode} |
| 531 |
\str_new:N \l_context_secondary_str |
| 532 |
% \end{macrocode} |
| 533 |
% \end{macro} |
| 534 |
% |
| 535 |
% |
| 536 |
% \begin{macro}{\SetSecondaryContext} |
| 537 |
% Similar to \cs{SetPrimaryContext}. |
| 538 |
% \begin{macrocode} |
| 539 |
\cs_new_protected:Npn \SetSecondaryContext #1 { |
| 540 |
\str_set:Ne \l_context_secondary_str |
| 541 |
{ \tl_if_empty:nF { #1 } |
| 542 |
{ \cs_if_exist_use:cF |
| 543 |
{ g_@@_secondary_ \l_context_secondary_str _ #1 _tl } |
| 544 |
{ \cs_if_exist_use:cF |
| 545 |
{ g_@@_secondary_ * _ #1 _tl } |
| 546 |
{ #1 } |
| 547 |
} |
| 548 |
} |
| 549 |
} |
| 550 |
\@@_debug_typeout:n{set~ secondary~ <-~ \l_context_secondary_str } |
| 551 |
} |
| 552 |
% \end{macrocode} |
| 553 |
% \end{macro} |
| 554 |
% |
| 555 |
% |
| 556 |
% \begin{macro}{\DeclareSecondaryContextRule} |
| 557 |
% |
| 558 |
% \begin{macrocode} |
| 559 |
\cs_new_protected:Npn \DeclareSecondaryContextRule #1#2#3 { |
| 560 |
\tl_gclear_new:c { g_@@_secondary_ #1 _ #2 _tl } |
| 561 |
\tl_gset:cn { g_@@_secondary_ #1 _ #2 _tl } {#3} |
| 562 |
} |
| 563 |
% \end{macrocode} |
| 564 |
% \end{macro} |
| 565 |
% |
| 566 |
% |
| 567 |
% |
| 568 |
% \subsection{Supporting font size changes as a secondary context} |
| 569 |
% |
| 570 |
% |
| 571 |
% |
| 572 |
% \begin{macro}{\@setfontsize} |
| 573 |
% We try to be careful when setting the name as we don't know if |
| 574 |
% \cs{string} returns a backslash or not. |
| 575 |
% \begin{macrocode} |
| 576 |
\def\@setfontsize#1#2#3{\@nomath#1% |
| 577 |
\ifx\protect\@typeset@protect |
| 578 |
\let\@currsize#1% |
| 579 |
\begingroup |
| 580 |
\escapechar\m@ne |
| 581 |
\expandafter |
| 582 |
\endgroup |
| 583 |
\expandafter |
| 584 |
\SetSecondaryContext |
| 585 |
\expandafter {\string#1}% |
| 586 |
\fi |
| 587 |
\fontsize{#2}{#3}\selectfont |
| 588 |
} |
| 589 |
% \end{macrocode} |
| 590 |
% If we are typesetting in \cs{normalsize} we don't want that as as |
| 591 |
% the context, so here is a first application of a rule. |
| 592 |
% \begin{macrocode} |
| 593 |
|
| 594 |
\DeclareSecondaryContextRule{*}{normalsize}{} |
| 595 |
% \end{macrocode} |
| 596 |
% \end{macro} |
| 597 |
% |
| 598 |
% |
| 599 |
% |
| 600 |
% \subsection{Supporting primary context} |
| 601 |
% |
| 602 |
% \subsubsection{Float context} |
| 603 |
% |
| 604 |
% \begin{macro}{\@float} |
| 605 |
% This should work with most float pages. There are of course some |
| 606 |
% classes or packages that produce pseudo floats without calling |
| 607 |
% \cs{@float}. These need to be handled separately by adding a |
| 608 |
% \cs{SetPrimaryContext} in their code. |
| 609 |
% \begin{macrocode} |
| 610 |
\AddToHook{cmd/@float/before}{\SetPrimaryContext{float}} |
| 611 |
% \end{macrocode} |
| 612 |
% \end{macro} |
| 613 |
% |
| 614 |
% |
| 615 |
% \subsubsection{Caption context} |
| 616 |
% |
| 617 |
% \TODO{in latex-lab-float} |
| 618 |
% \cs{@makecaption} is redefined there and the context should be set in that |
| 619 |
% command (or its replacement one day). |
| 620 |
% |
| 621 |
% \TODO{longtable and anything else that runs around producing \cs{caption}s} |
| 622 |
% |
| 623 |
% |
| 624 |
% \subsubsection{Footnote context} |
| 625 |
% |
| 626 |
% \TODO{in latex-lab-footnote} |
| 627 |
% |
| 628 |
% |
| 629 |
% |
| 630 |
% \subsubsection{Header context} |
| 631 |
% |
| 632 |
% \TODO{} |
| 633 |
% |
| 634 |
% |
| 635 |
% |
| 636 |
% \subsubsection{Footer context} |
| 637 |
% |
| 638 |
% \TODO{} |
| 639 |
% |
| 640 |
% |
| 641 |
% |
| 642 |
% \subsubsection{Marginal context} |
| 643 |
% |
| 644 |
% \TODO{} |
| 645 |
% |
| 646 |
% |
| 647 |
% |
| 648 |
% |
| 649 |
% |
| 650 |
% \subsection{Changes to lttemplates.dtx code} |
| 651 |
% |
| 652 |
% \begin{macrocode} |
| 653 |
%<@@=template> |
| 654 |
% \end{macrocode} |
| 655 |
% |
| 656 |
% |
| 657 |
% |
| 658 |
% |
| 659 |
% \begin{macro}{\@@_use_instance_aux:nn} |
| 660 |
% This is the command that is used by \cs{UseInstance} to select and |
| 661 |
% execute a requested instance. So we now have to have a little |
| 662 |
% more logic here. |
| 663 |
|
| 664 |
% In the normal case (both primary and secondary context are empty) |
| 665 |
% we now have 3 tests. We have up to 4 tests if only primary is |
| 666 |
% non-empty and up to 5 tests if secondary is non-empty. |
| 667 |
% \begin{macrocode} |
| 668 |
\cs_set_protected:Npn \@@_use_instance_aux:nn #1#2 { |
| 669 |
\@@_debug:n { \str_if_empty:NF \l_context_primary_str |
| 670 |
{ \@@_debug_typeout:n {primary~ context~ is~ |
| 671 |
'\l_context_primary_str' } } } |
| 672 |
\@@_debug:n { \str_if_empty:NF \l_context_secondary_str |
| 673 |
{ \@@_debug_typeout:n {secondary~ context~ is~ |
| 674 |
'\l_context_secondary_str' } } } |
| 675 |
\tl_if_empty:NTF \l__context_secondary_tl |
| 676 |
{ |
| 677 |
\str_if_empty:NTF \l_context_primary_str |
| 678 |
{ |
| 679 |
\@@_if_instance_exist:nnTF { #1 } { #2 } |
| 680 |
{ \@@_use_existing_instance:nn { #1 } { #2 } } |
| 681 |
{ \msg_error:nnnn { template } { unknown-instance } {#1} {#2} } |
| 682 |
} |
| 683 |
{ |
| 684 |
\@@_if_instance_exist:nnTF { #1 } { #2 : \l_context_primary_str } |
| 685 |
{ \@@_use_existing_instance:nn { #1 } |
| 686 |
{ #2 : \l_context_primary_str } } |
| 687 |
{ |
| 688 |
\@@_if_instance_exist:nnTF { #1 } { #2 } |
| 689 |
{ \@@_use_existing_instance:nn { #1 } { #2 } } |
| 690 |
{ \msg_error:nnnn { template } { unknown-instance } {#1} {#2} } |
| 691 |
} |
| 692 |
} |
| 693 |
} |
| 694 |
{ |
| 695 |
\@@_if_instance_exist:nnTF { #1 } |
| 696 |
{ #2 : \l_context_primary_str |
| 697 |
: \l_context_secondary_str } |
| 698 |
{ \@@_use_existing_instance:nn { #1 } |
| 699 |
{ #2 : \l_context_primary_str : \l_context_secondary_str } } |
| 700 |
{ |
| 701 |
\str_if_empty:NTF \l_context_primary_str |
| 702 |
{ |
| 703 |
\@@_if_instance_exist:nnTF { #1 } { #2 } |
| 704 |
{ \@@_use_existing_instance:nn { #1 } { #2 } } |
| 705 |
{ \msg_error:nnnn { template } { unknown-instance } {#1} {#2} } |
| 706 |
} |
| 707 |
{ |
| 708 |
\@@_if_instance_exist:nnTF { #1 } |
| 709 |
{ #2 : \l_context_primary_str } |
| 710 |
{ \_@@_use_existing_instance:nn { #1 } |
| 711 |
{ #2 : \l_context_primary_str } } |
| 712 |
{ |
| 713 |
\@@_if_instance_exist:nnTF { #1 } { #2 } |
| 714 |
{ \@@_use_existing_instance:nn { #1 } { #2 } } |
| 715 |
{ \msg_error:nnnn { template } { unknown-instance } {#1} {#2} } |
| 716 |
} |
| 717 |
} |
| 718 |
} |
| 719 |
} |
| 720 |
} |
| 721 |
% \end{macrocode} |
| 722 |
% \end{macro} |
| 723 |
% |
| 724 |
% |
| 725 |
% |
| 726 |
% |
| 727 |
% \begin{macro}{\_@@_use_existing_instance:nn} |
| 728 |
% We combine all debugging info for an execution of the instance in one |
| 729 |
% macro, to be used when we know for sure that this instance |
| 730 |
% exists. |
| 731 |
% \begin{macrocode} |
| 732 |
\cs_new_protected:Npn \_@@_use_existing_instance:nn #1#2 { |
| 733 |
\@@_debug_typeout:n{use~ '#1'~ instance:~ #2 \on@line } |
| 734 |
\use:c { \c_@@_instances_root_tl #1 / #2 } |
| 735 |
} |
| 736 |
% \end{macrocode} |
| 737 |
% \end{macro} |
| 738 |
% |
| 739 |
% |
| 740 |
% \subsubsection{Debugging of templates} |
| 741 |
% |
| 742 |
% |
| 743 |
% \begin{variable}{\g_@@_debug_bool} |
| 744 |
% |
| 745 |
% \begin{macrocode} |
| 746 |
\bool_new:N \g_@@_debug_bool |
| 747 |
% \end{macrocode} |
| 748 |
% \end{variable} |
| 749 |
% |
| 750 |
% |
| 751 |
% \begin{macro}{\@@_debug:n,\@@_debug_typeout:n} |
| 752 |
% |
| 753 |
% \begin{macrocode} |
| 754 |
\cs_new_eq:NN \@@_debug:n \use_none:n |
| 755 |
\cs_new_eq:NN \@@_debug_typeout:n \use_none:n |
| 756 |
% \end{macrocode} |
| 757 |
% \end{macro} |
| 758 |
% |
| 759 |
% \begin{macro}{\template_debug_on:,\template_debug_off:, |
| 760 |
% \@@_debug_gset:} |
| 761 |
% \begin{macrocode} |
| 762 |
\cs_new_protected:Npn \template_debug_on: |
| 763 |
{ |
| 764 |
\bool_gset_true:N \g_@@_debug_bool |
| 765 |
\@@_debug_gset: |
| 766 |
} |
| 767 |
% \end{macrocode} |
| 768 |
% |
| 769 |
% \begin{macrocode} |
| 770 |
\cs_new_protected:Npn \template_debug_off: |
| 771 |
{ |
| 772 |
\bool_gset_false:N \g_@@_debug_bool |
| 773 |
\@@_debug_gset: |
| 774 |
} |
| 775 |
% \end{macrocode} |
| 776 |
% |
| 777 |
% \begin{macrocode} |
| 778 |
\cs_new_protected:Npn \@@_debug_gset: |
| 779 |
{ |
| 780 |
\cs_gset_protected:Npx \@@_debug:n ##1 |
| 781 |
{ \bool_if:NT \g_@@_debug_bool {##1} } |
| 782 |
\cs_gset_protected:Npx \@@_debug_typeout:n ##1 |
| 783 |
{ \bool_if:NT \g_@@_debug_bool { \typeout{[Template]~ ==>~ ##1} } } |
| 784 |
} |
| 785 |
% \end{macrocode} |
| 786 |
% \end{macro} |
| 787 |
% |
| 788 |
% |
| 789 |
% \begin{macro}{\DebugTemplatesOn,\DebugTemplatesOff} |
| 790 |
% |
| 791 |
% \begin{macrocode} |
| 792 |
\cs_new_protected:Npn \DebugTemplatesOn { \template_debug_on: } |
| 793 |
\cs_new_protected:Npn \DebugTemplatesOff { \template_debug_off: } |
| 794 |
% \end{macrocode} |
| 795 |
% |
| 796 |
% \begin{macrocode} |
| 797 |
\DebugTemplatesOn |
| 798 |
% \end{macrocode} |
| 799 |
% \end{macro} |
| 800 |
% |
| 801 |
% |
| 802 |
% |
| 803 |
% |
| 804 |
% \begin{macrocode} |
| 805 |
%</package> |
| 806 |
% \end{macrocode} |
| 807 |
% |
| 808 |
% |
| 809 |
% |
| 810 |
% |
| 811 |
% \begin{macrocode} |
| 812 |
%<*latex-lab> |
| 813 |
\ProvidesFile{context-latex-lab-testphase.ltx} |
| 814 |
[\ltlabcontextdate\space v\ltlabcontextversion\space |
| 815 |
latex-lab wrapper context] |
| 816 |
|
| 817 |
\RequirePackage{latex-lab-testphase-context} |
| 818 |
|
| 819 |
%</latex-lab> |
| 820 |
% \end{macrocode} |