| 1 |
% \iffalse meta-comment |
| 2 |
% |
| 3 |
%% File: mathcolor.dtx (C) Copyright 2021-2022 |
| 4 |
% Frank Mittelbach, LaTeX Team |
| 5 |
% |
| 6 |
% It may be distributed and/or modified under the conditions of the |
| 7 |
% LaTeX Project Public License (LPPL), either version 1.3c of this |
| 8 |
% license or (at your option) any later version. The latest version |
| 9 |
% of this license is in the file |
| 10 |
% |
| 11 |
% https://www.latex-project.org/lppl.txt |
| 12 |
% |
| 13 |
% |
| 14 |
%%% From File: mathcolor.dtx |
| 15 |
% |
| 16 |
% \begin{macrocode} |
| 17 |
\def\mathcolorversion{v1.0c} |
| 18 |
\def\mathcolordate{2022/07/25} |
| 19 |
% \end{macrocode} |
| 20 |
%<*driver> |
| 21 |
\documentclass{l3doc} |
| 22 |
|
| 23 |
%\makeatletter \input{mathcolor.ltx} \makeatother |
| 24 |
|
| 25 |
\usepackage{color} |
| 26 |
|
| 27 |
% Fixing footnotes in functions and variables: this should be in l3doc! |
| 28 |
|
| 29 |
\newcommand\fixfootnote[2]{\footnotemark |
| 30 |
\AddToHookNext{env/#1/after}{\footnotetext{#2}}} |
| 31 |
\AddToHook{env/function/begin}{\def\footnote{\fixfootnote{function}}} |
| 32 |
\AddToHook{env/variable/begin}{\def\footnote{\fixfootnote{variable}}} |
| 33 |
|
| 34 |
\EnableCrossrefs |
| 35 |
\CodelineIndex |
| 36 |
\begin{document} |
| 37 |
\DocInput{mathcolor.dtx} |
| 38 |
\end{document} |
| 39 |
%</driver> |
| 40 |
% |
| 41 |
% \fi |
| 42 |
% |
| 43 |
% \title{Providing color in math\thanks{This file has version |
| 44 |
% \mathcolorversion\ dated \mathcolordate, \copyright\ \LaTeX\ |
| 45 |
% Project.}} |
| 46 |
% \author{Frank Mittelbach} |
| 47 |
% |
| 48 |
% \maketitle |
| 49 |
% |
| 50 |
% |
| 51 |
% \section{Introduction} |
| 52 |
% |
| 53 |
% It is possible to use the \cs{color} command in math formulas but |
| 54 |
% doing that has a number of restrictions and often leads to rather |
| 55 |
% unreadable input. For example, to color the summation sign in red |
| 56 |
% in the following formula |
| 57 |
% \[ |
| 58 |
% X = \color{red} \sum_{{\color{black} i=1}}^{{\color{black} n}} \color{black} x_i |
| 59 |
% \] |
| 60 |
% A total of four color commands and a number of seemingly |
| 61 |
% unnecessary extra braces in the sub and superscript are needed: |
| 62 |
%\begin{verbatim} |
| 63 |
% \[ X = \color{red} \sum |
| 64 |
% _{{\color{black} i=1}} % without {{ the superscript is misplaced |
| 65 |
% ^{{\color{black} n}} % without {{ the \sum is black |
| 66 |
% \color{black} % without the x_i is red |
| 67 |
% x_i \] |
| 68 |
%\end{verbatim} |
| 69 |
% While this is ugly but at least works, other things are simply |
| 70 |
% impossible, e.g., |
| 71 |
% \[ \left\{ \frac{1}{2} \mathcolor{red}{\right\}} \] |
| 72 |
% are not achievable at all because of the fact that \cs{left} and |
| 73 |
% \cs{right} form a group. Thus, a \cs{color} command immediately |
| 74 |
% before the \cs{right} has no effect and \verb=\}= remains black. |
| 75 |
% Putting the color change before the \cs{left} and then resetting |
| 76 |
% the color inside would work if you want to color both braces, but |
| 77 |
% the above combination or the attempt to color only the left brace |
| 78 |
% fails always. |
| 79 |
% |
| 80 |
% Using \cs{textcolor} in formulas appears to work on first glance, |
| 81 |
% but it produces spacing problems that are not easy to overcome |
| 82 |
% either, because it generates a single \cs{mathord} and no |
| 83 |
% longer reflects its input, thus |
| 84 |
% \verb/\[ a = b \textcolor{red}{\neq} c \]/ |
| 85 |
% produces |
| 86 |
% \[ a = b \textcolor{red}{\neq} c \] |
| 87 |
% This is a case one could mend by wrapping the |
| 88 |
% \cs{textcolor} and its arguments in a \cs{mathrel}, but even when |
| 89 |
% that is possible, the resulting formula source is difficult to |
| 90 |
% maintain and in other situations the solutions are even more |
| 91 |
% complicated or there is no solution at all. |
| 92 |
% |
| 93 |
% We therefore offer now a dedicated math coloring command named |
| 94 |
% \cs{mathcolor}. |
| 95 |
% |
| 96 |
% \begin{function}{\mathcolor} |
| 97 |
% \begin{syntax} |
| 98 |
% \cs{mathcolor} \oarg{model} \Arg{color-spec} \Arg{math material} |
| 99 |
% \end{syntax} |
| 100 |
% It has the same arguments as \cs{textcolor} but is intended for |
| 101 |
% use in formulas. The command does not generate a group and the |
| 102 |
% \meta{math material} retains its math atom states and it correctly |
| 103 |
% handles sub and superscripts that follow. |
| 104 |
% \end{function} |
| 105 |
% |
| 106 |
% The command can also be used to color a single opening or closing |
| 107 |
% symbol, e.g., the correct input to our earlier example is |
| 108 |
%\begin{verbatim} |
| 109 |
% \[ \left\{ \frac{1}{2} \mathcolor{red}{\right\}} \] |
| 110 |
%\end{verbatim} |
| 111 |
% which is how it was produced. |
| 112 |
% |
| 113 |
% If you attempt to color large operators that use explicit |
| 114 |
% \cs{limits}, \cs{nolimits}, or \cs{displaylimits}, then these |
| 115 |
% limit controls need to immediately follow the operator. However, |
| 116 |
% \cs{mathcolor} is written in a way that it is possible write |
| 117 |
%\begin{verbatim} |
| 118 |
% \[ \mathcolor{red}{\int}\limits_0^1 \textrm{ or } |
| 119 |
% \mathcolor{red}{\int\limits}_0^1 \] |
| 120 |
%\end{verbatim} |
| 121 |
% and achieve the same effect: |
| 122 |
% \[ \mathcolor{red}{\int}\limits_0^1 \textrm{ or } |
| 123 |
% \mathcolor{red}{\int\limits}_0^1 \] |
| 124 |
% |
| 125 |
% |
| 126 |
% \MaybeStop{\setlength\IndexMin{200pt} \PrintIndex } |
| 127 |
% |
| 128 |
% |
| 129 |
% \section{The Implementation} |
| 130 |
% |
| 131 |
% The code is called inside of the \pkg{color} or \pkg{xcolor} |
| 132 |
% packages, so \texttt{@} is already a letter, but the |
| 133 |
% coding here is done in the L3 programming layer, so we need to |
| 134 |
% activate that. But first we check if this file was loaded before, |
| 135 |
% e.g., when both \pkg{color} and \pkg{xcolor} are in the preamble |
| 136 |
% and in that case we stop immediately. |
| 137 |
% \begin{macrocode} |
| 138 |
%<*code> |
| 139 |
\ifcsname mathcolor\endcsname \endinput \fi |
| 140 |
% \end{macrocode} |
| 141 |
% |
| 142 |
% \begin{macrocode} |
| 143 |
\ExplSyntaxOn |
| 144 |
%<@@=mathcolor> |
| 145 |
% \end{macrocode} |
| 146 |
% |
| 147 |
% \begin{macro}{\g_@@_seq} |
| 148 |
% We need to keep our own stack of color commands, so we allocate a |
| 149 |
% sequence for that. |
| 150 |
% \begin{macrocode} |
| 151 |
\seq_new:N \g_@@_seq |
| 152 |
% \end{macrocode} |
| 153 |
% \end{macro} |
| 154 |
% |
| 155 |
% |
| 156 |
% \begin{macro}{\mathcolor} |
| 157 |
% The document-level command which expects a color (if unnamed then |
| 158 |
% the optional argument is the model) and colors the content of the |
| 159 |
% second argument (like \cs{textcolor}, but without spacing |
| 160 |
% problems in math). |
| 161 |
% \begin{macrocode} |
| 162 |
\DeclareDocumentCommand \mathcolor { o m m } |
| 163 |
{ |
| 164 |
% \end{macrocode} |
| 165 |
% The \cs{mathcolor} is only supported in math mode because in text |
| 166 |
% mode it has problems scanning away a space after it, for example. |
| 167 |
% We therefore raise an error if it executes |
| 168 |
% anywhere else. The \LaTeXe{} error command is a |
| 169 |
% bit strangely named, because in the kernel it is only used for |
| 170 |
% math alphabets, but the message it gives is fine. |
| 171 |
% \changes{v1.0b}{2022/01/28}{Restrict command to math mode} |
| 172 |
% \begin{macrocode} |
| 173 |
\mode_if_math:F { \non@alpherr {\mathcolor\space} } |
| 174 |
% \end{macrocode} |
| 175 |
% First real action is to save the current color value on a stack |
| 176 |
% (needed if the command is nested or contains some further color |
| 177 |
% changes with \cs{color} inside). |
| 178 |
% \begin{macrocode} |
| 179 |
\seq_gpush:No \g_@@_seq \current@color |
| 180 |
% \end{macrocode} |
| 181 |
% Then we switch to the new color, but we do not want to reset the |
| 182 |
% color after the group (which is done by \cs{color} using |
| 183 |
% \cs{aftergroup}\cs{reset@color}). The best solution here would be |
| 184 |
% if the color packages would provide a command doing just the |
| 185 |
% color switching, but for now we simply undo that part by pushing |
| 186 |
% \cs{use_none:n} which gobbles the \cs{reset@color} added by |
| 187 |
% \cs{color} with \cs{aftergroup}. |
| 188 |
% \begin{macrocode} |
| 189 |
\group_insert_after:N \use_none:n |
| 190 |
% \end{macrocode} |
| 191 |
% Switching the color is also slightly suboptimal, because |
| 192 |
% depending on whether or not we have a \meta{model} argument, we |
| 193 |
% have to call \cs{color} with or without the optional argument. But |
| 194 |
% going low-level here is not an option as we need to support |
| 195 |
% different color packages and their internals are not identical. |
| 196 |
% \begin{macrocode} |
| 197 |
\IfValueTF {#1} { \color[#1]{#2} } { \color{#2} } |
| 198 |
% \end{macrocode} |
| 199 |
% Then comes the math material we want to see colored: |
| 200 |
% \begin{macrocode} |
| 201 |
#3 |
| 202 |
% \end{macrocode} |
| 203 |
% After that we need to reset the color ourselves (without a group |
| 204 |
% that does it for us), i.e., popping the saved color from our |
| 205 |
% stack, but there are some twists to that so we do this in a |
| 206 |
% separate command (which in fact needs to be called several times, |
| 207 |
% so inlining the code wouldn't be possible. |
| 208 |
% \begin{macrocode} |
| 209 |
\@@_scan_for_scripts:w |
| 210 |
} |
| 211 |
% \end{macrocode} |
| 212 |
% \end{macro} |
| 213 |
% |
| 214 |
% |
| 215 |
% |
| 216 |
% \begin{macro}{\@@_scan_for_scripts:w} |
| 217 |
% The complication when changing the color back is due to the fact |
| 218 |
% that the \cs{mathcolor} may be followed by \verb=^= or \verb=_= |
| 219 |
% or the hidden superscript \texttt{'} and its argument may end in a |
| 220 |
% \cs{mathop} in which case the sub and superscripts may be |
| 221 |
% attached as \cs{limits} instead of after the material. All cases |
| 222 |
% need separate treatment. |
| 223 |
% And we need to watch out for an upcoming \verb=&= and avoid |
| 224 |
% prematurely triggering the end of an alignment cell. |
| 225 |
% \changes{v1.0c}{2022/07/25}{Avoid ending an alignment cell |
| 226 |
% prematurely when hitting an \texttt{\&} during |
| 227 |
% scanning (gh/901)} |
| 228 |
% \begin{macrocode} |
| 229 |
\cs_new_protected:Npn \@@_scan_for_scripts:w |
| 230 |
{ |
| 231 |
% \end{macrocode} |
| 232 |
% We need to look at what follows \cs{mathcolor}, and we need to do |
| 233 |
% that ignoring (and dropping) any spaces and \cs{relax} as \TeX{} |
| 234 |
% would do in normal math processing (for example before a subscript |
| 235 |
% token). We do this with expansion so that hidden sub or superscripts |
| 236 |
% in macros are still found as long as the macros are expandable. |
| 237 |
% \begin{macrocode} |
| 238 |
\peek_remove_filler:n |
| 239 |
{ |
| 240 |
% \end{macrocode} |
| 241 |
% To avoid problems hitting on \verb=&= we start with a |
| 242 |
% \cs{group_align_safe_begin:}. That has to be ended with |
| 243 |
% \cs{group_align_safe_end:} when the |
| 244 |
% danger (aka scanning) is over, which, due to the branching below, happens at |
| 245 |
% four different points, i.e., when the \cs{mathcolor} is |
| 246 |
% \begin{enumerate} |
| 247 |
% \item |
| 248 |
% followed by a \enquote{normal} token; |
| 249 |
% \item |
| 250 |
% followed by a braced sub/superscript; |
| 251 |
% \item |
| 252 |
% followed by an unbraced sub/superscript; |
| 253 |
% \item |
| 254 |
% followed by one of the \cs{limits} primitives. |
| 255 |
% \end{enumerate} |
| 256 |
% In each case we have to end the align safe group and we mark the |
| 257 |
% points below in the code for easy reference. |
| 258 |
% \begin{macrocode} |
| 259 |
\group_align_safe_begin: |
| 260 |
% \end{macrocode} |
| 261 |
% We first parse for \cs{c_math_subscript_token} or |
| 262 |
% \cs{c_math_superscript_token}. |
| 263 |
% After \cs{peek_remove_filler:n} is done, it sets \cs{l_peek_token} |
| 264 |
% equal to the next non-filler token, so we can avoid unnecessary |
| 265 |
% work and just compare that. If either of the tokens is found, call |
| 266 |
% \cs{@@_handle_scripts:Nw}: |
| 267 |
% \begin{macrocode} |
| 268 |
\token_case_catcode:NnTF \l_peek_token |
| 269 |
{ |
| 270 |
\c_math_subscript_token { } |
| 271 |
\c_math_superscript_token { } |
| 272 |
} |
| 273 |
{ \@@_handle_scripts:Nw } |
| 274 |
% \end{macrocode} |
| 275 |
% Otherwise we check if it was any of the limit operation |
| 276 |
% primitives. If that is the case, e.g., if we have a situation |
| 277 |
% such as |
| 278 |
%\begin{verbatim} |
| 279 |
% \mathcolor{red}{\int}\limits_1 |
| 280 |
%\end{verbatim} |
| 281 |
% we have to move it directly after the \cs{int} to ensure there |
| 282 |
% is no color reset between the operator and the \cs{limits} command. |
| 283 |
% \begin{macrocode} |
| 284 |
{ |
| 285 |
\token_case_meaning:NnTF \l_peek_token |
| 286 |
{ |
| 287 |
\limits { \limits } |
| 288 |
\nolimits { \nolimits } |
| 289 |
\displaylimits { \displaylimits } |
| 290 |
} |
| 291 |
% \end{macrocode} |
| 292 |
% Once that is done, we have to get rid of the token we peeked at |
| 293 |
% and then restart scanning for sub or superscripts. Given that |
| 294 |
% \cs{@@_scan_for_scripts:w} expands while scanning the simplest |
| 295 |
% solution is to add \cs{use_none:n} in front of the peeked at |
| 296 |
% token. |
| 297 |
% |
| 298 |
% Here we end the align safe group and |
| 299 |
% \cs{@@_scan_for_scripts:w} will start a new one. |
| 300 |
% \begin{macrocode} |
| 301 |
{ |
| 302 |
\group_align_safe_end: % case 4 |
| 303 |
\@@_scan_for_scripts:w \use_none:n |
| 304 |
} |
| 305 |
% \end{macrocode} |
| 306 |
% If it was not one of these we look for a \texttt{'} and if found remove it and |
| 307 |
% replace it by its expansion. The reason we have to do this (and |
| 308 |
% not rely on the earlier peeking to expand for us is the fact |
| 309 |
% that \texttt{'} is only ``math active'' and that doesn't expand |
| 310 |
% under \cs{expanded} or \cs{expandafter}. |
| 311 |
% \begin{macrocode} |
| 312 |
{ |
| 313 |
\token_if_eq_meaning:NNTF \l_peek_token ' |
| 314 |
{ |
| 315 |
\@@_handle_scripts:Nw ^ |
| 316 |
\c_group_begin_token |
| 317 |
\exp_after:wN \prim@s \use_none:n |
| 318 |
} |
| 319 |
% \end{macrocode} |
| 320 |
% If it is anything else we finish off which means we reset the |
| 321 |
% color (because we prevented that before to happen automatically |
| 322 |
% after the next group) and pop the color stack setting \cs{current@color}. |
| 323 |
% \begin{macrocode} |
| 324 |
{ |
| 325 |
\group_align_safe_end: % case 1 |
| 326 |
\reset@color |
| 327 |
\seq_gpop:NN \g_@@_seq \current@color |
| 328 |
} |
| 329 |
} |
| 330 |
} |
| 331 |
} |
| 332 |
} |
| 333 |
% \end{macrocode} |
| 334 |
% \end{macro} |
| 335 |
% |
| 336 |
% |
| 337 |
% \begin{macro}{\@@_handle_scripts:Nw} |
| 338 |
% The tricky part of handling sub and superscripts is that we have |
| 339 |
% to reset color to the one that is on the stack but reset it back |
| 340 |
% to what it was before to allow for cases like |
| 341 |
%\begin{verbatim} |
| 342 |
% \[ \mathcolor{red}{a+\sum}_{i=1}^n \] |
| 343 |
%\end{verbatim} |
| 344 |
% Here, \TeX{} constructs a \cs{vbox} stacking subscript, summation |
| 345 |
% sign, and superscript. So technically the superscript comes first |
| 346 |
% and the \cs{sum} that should get colored red is the middle. |
| 347 |
% \begin{macrocode} |
| 348 |
\cs_new_protected:Npn \@@_handle_scripts:Nw #1 |
| 349 |
{ |
| 350 |
% \end{macrocode} |
| 351 |
% The argument is either \verb=^= or \verb=_=, so we execute it and |
| 352 |
% explicitly open two \verb={= groups. We need two because color |
| 353 |
% resets are always done after a group, so the first group is for |
| 354 |
% script material (in case it was just something like \verb=_i=) |
| 355 |
% and the second is needed for the color reset to keep it within |
| 356 |
% the super or subscript. If that reset would happen after it then |
| 357 |
% the color \cs{special} would interfere with \TeX{} math spacing. |
| 358 |
% \begin{macrocode} |
| 359 |
#1 \c_group_begin_token \c_group_begin_token |
| 360 |
% \end{macrocode} |
| 361 |
% Now it is time to change the color back to the one on the stack. |
| 362 |
% \begin{macrocode} |
| 363 |
\seq_get:NN \g_@@_seq \current@color |
| 364 |
\set@color |
| 365 |
% \end{macrocode} |
| 366 |
% \cs{set@color} adds \cs{aftergroup}\cs{reset@color}. We now a add |
| 367 |
% bit more so that the code executed after the current (inner) group |
| 368 |
% looks like this: |
| 369 |
%\begin{verbatim} |
| 370 |
% \reset@color } \@@_scan_for_scripts:w |
| 371 |
%\end{verbatim} |
| 372 |
% The \cs{@@_scan_for_scripts:w} then retakes control and initiates |
| 373 |
% parsing for another sub or superscript. |
| 374 |
% \begin{macrocode} |
| 375 |
\group_insert_after:N \c_group_end_token |
| 376 |
\group_insert_after:N \@@_scan_for_scripts:w |
| 377 |
% \end{macrocode} |
| 378 |
% Before we give control to \TeX{} to process the sub or |
| 379 |
% superscript some final adjustment is necessary: if the input was |
| 380 |
% \verb=^{...}= then we have one \verb={= too many, because we |
| 381 |
% already supplied the outer one already. In that case we drop |
| 382 |
% it. Otherwise we have an unbraced single token sub or superscript |
| 383 |
% which means we are missing a closing \verb=}= at the end and need |
| 384 |
% to account for that: this is done in false branch by |
| 385 |
% \cs{use_ii_i:nn}. |
| 386 |
% |
| 387 |
% After scanning for a brace all scanning is done, so here are the |
| 388 |
% other two points where we have to end the align safe group (in |
| 389 |
% the true and false case). |
| 390 |
% \begin{macrocode} |
| 391 |
\peek_remove_filler:n |
| 392 |
{ |
| 393 |
\token_if_eq_meaning:NNTF \l_peek_token \c_group_begin_token |
| 394 |
{ |
| 395 |
\group_align_safe_end: % case 2 |
| 396 |
\peek_catcode_remove:NT \c_group_begin_token { } |
| 397 |
} |
| 398 |
{ |
| 399 |
\exp_after:wN \group_align_safe_end: % case 3 |
| 400 |
\use_ii_i:nn \c_group_end_token |
| 401 |
} |
| 402 |
} |
| 403 |
} |
| 404 |
% \end{macrocode} |
| 405 |
% \end{macro} |
| 406 |
% |
| 407 |
% \begin{macrocode} |
| 408 |
%<@@=> |
| 409 |
% \end{macrocode} |
| 410 |
% |
| 411 |
% \begin{macrocode} |
| 412 |
\ExplSyntaxOff |
| 413 |
%</code> |
| 414 |
% \end{macrocode} |
| 415 |
% |
| 416 |
% \Finale |
| 417 |
% |
| 418 |
% |
| 419 |
%%%%%%%%%%%%%%%% |
| 420 |
\endinput |
| 421 |
%%%%%%%%%%%%%%%% |
| 422 |
|