5

I decided to hack together an answer by using an existing answer: https://tex.stackexchange.com/a/127507/49339

So, my MnotWE looks like this, after much simplification:

\documentclass[12pt,letterpaper]{article}

\usepackage{lmodern}
\usepackage[T1]{fontenc}
\usepackage{amsmath}

\makeatletter
% LaTeX's \@ifnextchar gobbles spaces, therefore
% \msh@ifnextchar is defined that keeps spaces
\newcommand*{\msh@ifnextchar}[3]{%
    \def\msh@temp{\msh@@ifnextchar{#1}{#2}{#3}}%
    \futurelet\msh@token\msh@temp
}
\newcommand*{\msh@@ifnextchar}[1]{%
    \ifx\msh@token#1%
    \expandafter\@firstoftwo
    \else
    \expandafter\@secondoftwo
    \fi
}

% Commands that take the original meanings of the special characters
\mathchardef\msh@code@minus=\mathcode`\-\relax
\mathchardef\msh@code@plus=\mathcode`\+\relax

% Macro \setmathshorthands activates and defines the special
% characters
\begingroup
\@ifdefinable{\setmathshorthands}{%
    \xdef\setmathshorthands{%
        \let\noexpand-\noexpand\msh@minus
        \let\noexpand+\noexpand\msh@plus
    }%
}%
\endgroup

\newcommand*{\msh@minus}{\&}

\newcommand*{\msh@plus}{\$}

\makeatother

% Activate math shorthands in the math modes
\everymath{\setmathshorthands}
\everydisplay{\setmathshorthands}


\begin{document}
    \begin{equation*}
        a + b - c = 0
    \end{equation*}
\end{document}

The macros I am using at the moment are toy examples, I have some more complicated macros I will eventually replace these with. However, for the moment, let's just work with these toys.

Anyway, compiling this example, I get the error:

line 50: Missing control sequence inserted. \begin{equation*}

What do?

4
  • For one, you forgot \catcode ` \-=\active \catcode ` \+=\active Sep 19, 2017 at 2:07
  • the only way i can think of to do this is to make + and - active, and what's done after that involves some reasonably hairy munging about with primitive tex code. might be "safe" for the +, but my instincts say that for latex (or even probably plain tex) it's a bad idea for the minus if you have any text that might contain hyphens. Sep 19, 2017 at 2:11
  • @barbarabeeton you mean, if i have something like this? $\text{hyper-active}$, then that might break if I am doing the above? isn't that situation prevented by restricting this action only to math mode though?
    – bzm3r
    Sep 19, 2017 at 2:13
  • @user89 -- that is indeed the sort of thing i mean. another example of a text hyphen that i've seen many times in the wild is $p$-adic. i'd use that to test in addition to a \hyphenation{...} example. Sep 19, 2017 at 2:27

1 Answer 1

8

In the group, you needed

  \catcode`\-=\active
  \catcode`\+=\active

and in \setmathshorthands, you needed

  \mathcode\number`\-="8000 %
  \mathcode\number`\+="8000 %

The \catcode changes are necessary so that - and + are [temporarily] treated as active characters, so as to define their math-active behavior in future invocations of \setmathshorthands.

Thanks to the OP for citing the \mathcode information at What is the differences between mathcode and catcode and how can I use mathcode?, which is a way to make these characters be treated as active in math mode only, even when they are not actually active:

\mathcode"8000 is a special code that is not looked up in the usual way. If a character has that mathcode, the definition of the active (catcode 13) token is used instead, even though the character itself is not active. this is used in plain and LaTeX to allow ' to work as a normal non-active apostrophe in text but in math it has catcode hex 8000 so the active definition is used, which expands to ^{\prime}

The MWE (which also tests the use of - in text mode):

\documentclass[12pt,letterpaper]{article}

\usepackage{lmodern}
\usepackage[T1]{fontenc}
\usepackage{amsmath}

\makeatletter
% LaTeX's \@ifnextchar gobbles spaces, therefore
% \msh@ifnextchar is defined that keeps spaces
\newcommand*{\msh@ifnextchar}[3]{%
    \def\msh@temp{\msh@@ifnextchar{#1}{#2}{#3}}%
    \futurelet\msh@token\msh@temp
}
\newcommand*{\msh@@ifnextchar}[1]{%
    \ifx\msh@token#1%
    \expandafter\@firstoftwo
    \else
    \expandafter\@secondoftwo
    \fi
}

% Commands that take the original meanings of the special characters
\mathchardef\msh@code@minus=\mathcode`\-\relax
\mathchardef\msh@code@plus=\mathcode`\+\relax

% Macro \setmathshorthands activates and defines the special
% characters
\begingroup
  \catcode`\-=\active
  \catcode`\+=\active
\@ifdefinable{\setmathshorthands}{%
    \xdef\setmathshorthands{%
      \mathcode\number`\-="8000 %
      \mathcode\number`\+="8000 %
        \let\noexpand-\noexpand\msh@minus
        \let\noexpand+\noexpand\msh@plus
    }%
}%
\endgroup

\newcommand*{\msh@minus}{\&}

\newcommand*{\msh@plus}{\$}

\makeatother

% Activate math shorthands in the math modes
\everymath{\setmathshorthands}
\everydisplay{\setmathshorthands}


\begin{document}
A-b--c---d dddddddddddd dddddddddddd ddddddddddddd dddddddd fffff\-ggg
    \begin{equation*}
        a + b - c = 0 \quad \text{ravings of a mad-man}
    \end{equation*}
\end{document}

enter image description here

11
  • Thank you for this. Would you mind explaining what your additions do?
    – bzm3r
    Sep 19, 2017 at 2:11
  • 1
    Check out the answer to this question: tex.stackexchange.com/a/109440/49339 -- it says that: > \mathcode"8000 is a special code that is not looked up in the usual way. If a character has that mathcode, the definition of the active (catcode 13) token is used instead, even though the character itself is not active. this is used in plain and LaTeX to allow ' to work as a normal non-active apostrophe in text but in math it has catcode hex 8000 so the active definition is used, which expands to ^{\prime}
    – bzm3r
    Sep 19, 2017 at 2:16
  • So maybe the ` \catcode\<+, ->=\active is not needed because we are doing \mathcode"8000? edit: testing shows that it is needed, not sure I understand why
    – bzm3r
    Sep 19, 2017 at 2:18
  • 1
    @user89 That would be correct. The \catcodes are made active inside a group in the preamble, meaning they are no longer active once the group is closed. But in that group, their "math-active" definitions are set, pointing to \msh@minus and \msh@plus. Sep 19, 2017 at 2:29
  • 1
    @user89 -- define a word with marked hyphenation points in the \hyphenation{...} and then put that word in the middle of text, unhyphenated. keep adding words (or spacers of some kind, like "xxxx" or even \hspaces) before it until the test word is forced to the right-hand edge of the line where it can't avoid being hyphenated. sorry i can't think offhand of a good word to test; my laptop does not have a usable tex system installed. Sep 19, 2017 at 2:46

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .