8

I want to colour all sub- and super-scripts automatically. Following this question I was able to generate the desired result:

enter image description here enter image description here

Using the example below.

However, activating the "_" is a problem for me as I have been using it extensively in file names, meaning this solution will typically fail for me when using "\input" and "\includegraphics".

Is there a way that I can activate the characters every time LaTeX enters math-mode?

Example

LaTeX file (typeset simply using pdflatex)

\documentclass{minimal}
\usepackage[active,tightpage,textmath]{preview}
\setlength\PreviewBorder{5pt}%
\usepackage{xcolor}

\newcommand{\amat}{\mathbf{A}}
\newcommand{\bmat}{\mathbf{B}}
\newcommand{\cmat}{\mathbf{C}}

\begin{document}

\(
  \cmat_{i}^{k} = \amat^{i}_{j} \bmat^{j}_{k}
\)

\catcode`_=\active
\catcode`^=\active
\newcommand_[1]{\ensuremath{\sb{\begingroup\color{magenta}#1\endgroup}}}
\newcommand^[1]{\ensuremath{\sp{\begingroup\color{cyan}#1\endgroup}}}

\(
  \cmat_{i}^{k} = \amat^{i}_{j} \bmat^{j}_{k}
\)

\end{document}
  • Does your document feature second-level subscripts or superscripts? I..e.,, something like A^{b_{c}}? And, may it be assumed that subscript and superscript material is always encased in curly braces? – Mico Dec 29 '16 at 07:49
  • Yes, but it is a special case and I am fine with the solution breaking down in those cases, although the document must still typeset. At this point in time the intended use is "for my eyes only" to make it easier to check my equations visually. – Sigve Karolius Dec 29 '16 at 07:56
  • Are you OK with a LuaLaTeX-based solution, or do you require pdfLaTeX? – Mico Dec 29 '16 at 08:01

2 Answers2

7

You are looking for 'math active' codes:

\documentclass{article}
\usepackage{color}

\newcommand{\amat}{\mathbf{A}}
\newcommand{\bmat}{\mathbf{B}}
\newcommand{\cmat}{\mathbf{C}}

\begingroup
  \catcode`_=\active
  \catcode`^=\active
  \gdef_#1{\sb{\begingroup\color{magenta}#1\endgroup}}
  \gdef^#1{\sp{\begingroup\color{cyan}#1\endgroup}}
\endgroup
\mathcode`\^="8000 %
\mathcode`\_="8000 %
\catcode`\^=12 %
\catcode`\_=12 %

\begin{document}

Some ^_ text.

\(
  \cmat_{i}^{k} = \amat^{i}_{j} \bmat^{j}_{k}
\)

\end{document}

This works as in math mode any character with mathcode "8000 is treated as an active char. The global definition of the behaviour of _ and ^ when active therefore applies even though in text mode they do nothing special.

Joseph Wright
  • 259,911
  • 34
  • 706
  • 1,036
  • couldn't one just mess with \sb and \sp? (just wondering) – daleif Dec 29 '16 at 09:05
  • @daleif Of course, but they are normally defined as being control word versions of the _ and ^ primitive behaviours, so would likely mess things up. Normally people want to keep their source using ^ and _ and to alter the output: if you are happy to alter your source then you can use different names anyway ... – Joseph Wright Dec 29 '16 at 09:07
  • Just curious: At the end of the preamble material, why do you reset the (non-mathmode) catcodes of ^ and _ to "12" (aka "other") rather than to their default values of "7" and "8", respectively? – Mico Dec 29 '16 at 13:06
  • 1
    @Mico The math active behavior is only triggered by character tokens of category code 11 or 12 – egreg Dec 29 '16 at 13:35
  • @egreg - Many thanks for this explanation. I wasn't aware of some of the finer points of math-active behavior... – Mico Dec 29 '16 at 14:01
4

Here's a LuaLaTeX-based solution. The main input syntax requirements are that (a) the sub- and superscript terms are always encased in curly braces and (b) the arguments of _ and ^ immediately follow these characters, i.e., intervening whitespace is not allowed. It is also assumed that if _ and ^ occur outside of math mode, say, in a URL string, these characters are not immediately followed by a left curly brace.

The solution doesn't modify the catcodes of _ and ^. Instead, it consists of a Lua function that uses Lua's powerful string.gsub function to perform the actual colorizing work and two LaTeX macros, named \sbspcolorsOn and \sbspcolorsOff, that activate and deactivate the operation of this Lua function.

enter image description here

\documentclass{article}
\usepackage{luacode,xcolor}
%% The Lua function 'sbspcolors' does most of the work:
\begin{luacode}
function sbspcolors ( s )
  s = string.gsub ( s , "%_(%b{})" , "_{\\textcolor{magenta}%1}" )
  s = string.gsub ( s , "%^(%b{})" , "^{\\textcolor{cyan}%1}" )
  return ( s )
end
\end{luacode}

%% Two helper macros to activate and deactivate the Lua function:
\newcommand{\sbspcolorsOn}{\directlua{luatexbase.add_to_callback(
   "process_input_buffer" , sbspcolors , "sbspcolors" )}}
\newcommand{\sbspcolorsOff}{\directlua{luatexbase.remove_from_callback(
   "process_input_buffer" , "sbspcolors" )}}

%% A few math-mode macros
\newcommand{\amat}{\mathbf{A}}
\newcommand{\bmat}{\mathbf{B}}
\newcommand{\cmat}{\mathbf{C}}

\begin{document}
$\cmat_{i_{u}}^{k^{\ell}} = \sum_{j=1}^{k} \amat^{i}_{j^{M}} \bmat^{j_{N}}_{k}$ \quad
\sbspcolorsOn  % activate the Lua function
$\cmat_{i_{u}}^{k^{\ell}} = \sum_{j=1}^{k} \amat^{i}_{j^{M}} \bmat^{j_{N}}_{k}$ \quad 
\sbspcolorsOff % deactivate the Lua function
$\cmat_{i_{u}}^{k^{\ell}} = \sum_{j=1}^{k} \amat^{i}_{j^{M}} \bmat^{j_{N}}_{k}$
\end{document} 
Mico
  • 506,678