1

I have this inline equation

$2^{\verb|N|-1}$

that gives this result

enter image description here

The problem is that using \verb|N| leaves N with the same size as in \displaystyle but I want it to be smaller.

How can I do?

  • 1
    why are you using \verb here? That seems fundamentally wrong. – David Carlisle Feb 21 '18 at 19:31
  • @DavidCarlisle Maybe, it is a code variable of a computer program. Such code variable names could have underscores, colons, ... But, I agree, type writer font is not the best choice for mathematical variables. Then just $2^{N-1}$ will do better. – Heiko Oberdiek Feb 21 '18 at 21:48
2

Two alternatives with automatically resized N:

\documentclass{article}
\usepackage{amstext}% or package amsmath for \text
\newcommand*{\ttmath}[1]{%
  \texttt{\mdseries\upshape#1}%
}

\begin{document}

  $2^{\verb|N|-1}$ {\small(\verb|\verb|)}

  $2^{\texttt{N}-1}$ {\small(\verb|\texttt| with \verb|amstext|)}

  \textit{Italics $2^{\ttmath{N}-1}$ context} {\small(macro \verb|\ttmath|)}

  $2^{\mathtt{N}-1}$ {\small(\verb|\mathtt|)}

\end{document}

Result

Remarks:

  • \texttt here works with automatically resizing in math mode because it internally uses \nfss@text that is redefined in package amstext as \text.

  • Macro \ttmath also resets font attributes series and shape to get independent from the current text font setting.

  • \mathtt is the most efficient command and uses the configured typewriter font for math. This is often the same as \ttfamily. Some font packages switch both fonts at the same time as lmodern, others do not as beramono. Therefore, it depends on the font setup, whether \mathtt can be used instead of \verb or \texttt.

| improve this answer | |
  • If used in a theorem statement (or other italic context), the second solution can lead to surprising results. By the way, \text is not necessary. In my opinion just \mathtt{N} should be suggested. – egreg Feb 21 '18 at 21:10
  • @egreg If the surrounding text is italics, then \upshape can be used (\text{\ttfamily\upshape N}). \mathtt and \ttfamily can be different fonts. Then, \mathtt cannot be used as substitute for \texttt. Answer updated. – Heiko Oberdiek Feb 21 '18 at 21:30
2

The LaTeX kernel defines \mathtt and nothing else is necessary. By the way, \verb should not be abused for printing in the typewriter font, usually \texttt is enough, whereas \verb is needed for printing TeX code with special characters.

\documentclass{article}

\begin{document}

$2^{\mathtt{N}-1}$

$2^{\mathtt{N}^{\mathtt{M}}}$

\end{document}

enter image description here

It's probably useful to define your own semantic command, say

\newcommand{\tvar}[1]{\mathtt{#1}}

and use $2^{\tvar{N}}$. This way you're not tied to the particular representation that you can change at any time by just modifying the definition.

It may happen that the choice of a different font set doesn't update \mathtt. The solution is simple and the advantage over other solutions involving \text is that this can be easily adapted to support \boldmath out of the box.

\documentclass{article}
\usepackage[T1]{fontenc} % necessary for beramono
\usepackage{amsmath}

\usepackage{beramono}

% update \mathtt to use the same font as \ttfamily
\DeclareMathAlphabet{\mathtt}{\encodingdefault}{\ttdefault}{m}{n}
% if the monospaced font also supports boldface (b or bx)
\SetMathAlphabet{\mathtt}{\encodingdefault}{\ttdefault}{b}{n}

\newtheorem{theorem}{Theorem}

\begin{document}

$2^{\mathtt{N}-1}$ and \texttt{N}

\begin{theorem}
Something about $2^{\mathtt{N}-1}$
\end{theorem}

\end{document}

enter image description here


Side note

Using \texttt is wrong, as exemplified by the following code.

\documentclass{article}
\usepackage{amsmath}

\newcommand{\tvar}[1]{\mathtt{#1}}

\newtheorem{theorem}{Theorem}

\begin{document}

\section*{Right}

$2^{\tvar{N}-1}$

\begin{theorem}
Something about $2^{\tvar{N}-1}$
\end{theorem}

\section*{Wrong}

$2^{\texttt{N}-1}$

\begin{theorem}
Something about $2^{\texttt{N}-1}$
\end{theorem}

\end{document}

enter image description here

| improve this answer | |
  • Try other font packages like beramono or libertine. Then the glyph forms of N differ for \verb|N|/\texttt{N} and \mathtt{N}. – Heiko Oberdiek Feb 21 '18 at 21:38
  • @HeikoOberdiek That's quite easy to solve anyhow. – egreg Feb 21 '18 at 21:38
0

A solution using this answer of Werner: https://tex.stackexchange.com/a/120694/120578

\documentclass{article}
\usepackage{verbatim}% http://ctan.org/pkg/verbatimes
\usepackage{pgf}
\makeatletter
\newcommand{\mverbatimfont}{\def\verbatim@font{\ttfamily}}%
\makeatother
\def\verbatimfont#1{\pgfmathsetmacro\bls{1.2*#1}\mverbatimfont\fontsize{#1}{\bls}\selectfont}


\begin{document}
\verbatimfont{6}
$2^{\verb|N|-1}$

\verbatimfont{9}
$\verb|N|^{\verbatimfont{6}\verb|N|-1}$
\end{document}

Output:

enter image description here

| improve this answer | |
  • 1
    Doubtful that the OP wants to have a smaller verbatim font size for all verbatim text including outside of sub- and superscripts. Consider the following formula: $\verb|N|^{\verb|N|-1}$ – Heiko Oberdiek Feb 21 '18 at 19:22
  • @HeikoOberdiek I thought that the OP really needed a verbatim environment and added o solution for that.... Anyway fixed it for such cases too with verbatim environment and manually changing the fontsize. I could automate it too, but seems that doesn't really want to use verbatim but just it's fontfamilly... If someone else need something like what I did automated he may ask for it – koleygr Feb 21 '18 at 19:36

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.