
答え1
自動的にサイズが変更される 2 つの選択肢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}
備考:
\texttt
\nfss@text
ここでは、パッケージ内で として再定義されている をamstext
内部的に使用するため、数式モードで自動的にサイズ変更されます\text
。マクロは、
\ttmath
現在のテキスト フォント設定から独立するように、フォント属性のシリーズと形状もリセットします。\mathtt
は最も効率的なコマンドであり、設定されたタイプライター フォントを数式に使用します。これは多くの場合、 と同じです\ttfamily
。フォント パッケージによっては、 のように両方のフォントを同時に切り替えるものもあればlmodern
、 のように切り替えないものもあります。したがって、または の代わりに を使用できるberamono
かどうかは、フォントの設定によって異なります。\mathtt
\verb
\texttt
答え2
LaTeX カーネルが定義し\mathtt
、他には何も必要ありません。ちなみに、 は\verb
タイプライター フォントでの印刷に乱用すべきではなく、通常はこれ\texttt
で十分ですが、\verb
特殊文字を含む TeX コードを印刷するには が必要です。
\documentclass{article}
\begin{document}
$2^{\mathtt{N}-1}$
$2^{\mathtt{N}^{\mathtt{M}}}$
\end{document}
独自のセマンティックコマンドを定義すると便利でしょう。例えば、
\newcommand{\tvar}[1]{\mathtt{#1}}
そして を使用します$2^{\tvar{N}}$
。この方法では、特定の表現に縛られることなく、定義を変更するだけでいつでも変更できます。
別のフォント セットを選択すると更新されない場合があります。解決策は簡単で、他の解決策に比べて、すぐに使用できるように簡単に調整できるという\mathtt
利点があります。\text
\boldmath
\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}
サイドノート
\texttt
次のコードに示すように、の使用は誤りです。
\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}
答え3
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}
出力: