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?

bzm3r
  • 3,196
  • For one, you forgot \catcode \-=\active \catcode +=\active – Steven B. Segletes Sep 19 '17 at 02: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. – barbara beeton Sep 19 '17 at 02: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 '17 at 02: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. – barbara beeton Sep 19 '17 at 02:27

1 Answers1

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

  • Thank you for this. Would you mind explaining what your additions do? – bzm3r Sep 19 '17 at 02:11
  • 1
    Check out the answer to this question: https://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 '17 at 02:16
  • So maybe the \catcode<+, ->=\activeis not needed because we are doing\mathcode"8000`? edit: testing shows that it is needed, not sure I understand why – bzm3r Sep 19 '17 at 02: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. – Steven B. Segletes Sep 19 '17 at 02:29
  • please try out some additional text hyphens; there are some suggestions in comments to the original question. – barbara beeton Sep 19 '17 at 02:31
  • @barbarabeeton I tried out the text hyphen examples, and it seems to work. Can you provide some more information regarding how to set up a \hyphenation{...} example? I have never used that command before. From some searching, I see that it is used to specify where a split occurs, if a word needs to be split, but how do I force a word to be split in an example? – bzm3r Sep 19 '17 at 02:37
  • @barbarabeeton Not sure what you allude to. the following seems to typeset fine: A-b--c---d dddddddddddd dddddddddddd ddddddddddddd dddddddd fffff\-ggg – Steven B. Segletes Sep 19 '17 at 02:38
  • @StevenB.Segletes I have updated your answer with an expanded example to test out the scenarios barbara is suggesting, including an updated picture. It's awaiting peer review. – bzm3r Sep 19 '17 at 02:40
  • 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. – barbara beeton Sep 19 '17 at 02:46
  • @barbarabeeton I did \hyphenation{su-per-cal-i-frag-i-list-ic-ex-pi-al-a-do-cious} and then began the document with A-b--c---d dddddddddddd dddddddddddd ddddddddddddd supercalifragilisticexpialadocious. Everything looks good (I think). – Steven B. Segletes Sep 19 '17 at 02:51
  • @barbarabeeton thanks for these suggestions! – bzm3r Sep 19 '17 at 03:51