1

How do I detect whether or not I'm in teletype mode in LaTeX? I'm looking for something like \ifmmode or \mathpalette or \mathchoice.

Perhaps this is an xy problem, so let me give my original context: I'd like to define versions of some operators that work well in texttt. For example, I'd like a version of \vdash that works well enough in a monospaced font. Here I can define separate ones for tt mode and normal mode, but I'd like a single command that picks which one to use depending on the mode:

\documentclass{article}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{mathtools}
\usepackage{ifthen}
\usepackage{calc}
\newcommand{\aswidthof}[2]{\rlap{#1}\hphantom{#2}}
\makeatletter
\newlength{\aswidthof@w}
\newcommand{\aswidthofc}[2]{%
  \settowidth{\aswidthof@w}{#2}%
  \hspace{\dimexpr\aswidthof@w/2\relax}%
  \ifthenelse{\boolean{mmode}}{\mathclap{#1}}{\clap{#1}}%
  \hspace{\dimexpr\aswidthof@w/2\relax}%
}
\makeatother
\newcommand{\bettervdash}{\aswidthofc{\ensuremath{\vdash}}{\text{|-}}}
\begin{document}
\noindent \texttt{abcdefghijklmnopqrstuvwxyz} \\
\noindent \texttt{a$\vdash$cdefghi$\vdash$klmnopqrstuvwxyz} \\
\noindent \texttt{abcdefghijklmnopqrstuvwxyz} \\
\noindent \texttt{a\bettervdash cdefghi\bettervdash klmnopqrstuvwxyz} \\
\noindent \texttt{abcdefghijklmnopqrstuvwxyz} \\
\noindent $abc\bettervdash def$ \\
\noindent $abc\vdash def$ \\
\end{document}

rendered document

Thoughts I've had:

  1. I can try to parse \the\font, looking for tt
  2. I can condition on whether \widthof{i} and \widthof{m} are equal

Is there a better way? If not, is there reason to believe that one of these will work more uniformly than the other?

  • 1
    there is certainly no guarantee that a monospace font will have tt in its name Courier for example, or Consolas as used in this site – David Carlisle Jul 18 at 20:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.