# bracket as ordinary symbol in math mode

I would like to to write in math mode using tt font and everything ok, but brackets. The next code

$\mathtt{[x + y]}$


get

but i would like to get

.

It can be done by some additional commands

$\mathtt{\texttt{[}x + y\texttt{]}}$


Is there any way get desirable result without any additions?

I try to write definition for active symbol

\def\lbr{[}
\begingroup
\catcode[=\active
\gdef[{\texttt{[}}
\endgroup
\AtBeginDocument{\mathcode[="8000}


but it looks like replacement performs recursively.

Another way i think that it is necessary to change mathcode for [ to mathcode of letters but do not find solution.

Addition

The solution

\def\lbr{[}
\begingroup
\catcode[=\active
\gdef[{\texttt{\lbrack}}
\endgroup
\AtBeginDocument{\mathcode[="8000}


works correctly with \gdef[{\texttt{\lbrack}}

• welcome to tex.sx. if you're representing math with the \tt font, it's not likely to look a lot different than if your simply set it all as \texttt{[x + y]}. you might try that to see if it's acceptable. – barbara beeton Sep 20 '17 at 16:18
• @barbarabeeton thank you! yes! but i would like to write $\mathtt{\int f[x] dx}$ – dann Sep 20 '17 at 16:29
• that's harder. first, you need a font with a \tt integral. (would have been useful to have that requirement in the question.) if a suitable font can be found that is accessed by unicode, it might still be possible to treat it as \texttt with utf8 encoding. but this too falls apart as soon as you want non-numeric superscripts or subscripts -- that really does need math markup. i have to leave that to someone else to address. – barbara beeton Sep 20 '17 at 17:00
• if you load package mathastext it will do that (see 1.8 Extended scope of the math alphabets commands in its manual), il you emit \MTnonlettersobeymathxx. – user4686 Sep 20 '17 at 17:19
• @jfbu i ve just find tex.stackexchange.com/questions/119879/math-symbols-in-tt-font tank you!! – dann Sep 21 '17 at 13:14

## 2 Answers

You have to store the mathcode of [ and ], when you want to use them in their definitions as active characters.

Of course, the extended brackets cannot be from the monospaced font.

\documentclass{article}
\usepackage{amsmath}

\DeclareSymbolFont{mathttfont}{OT1}{\ttdefault}{m}{n}
\DeclareSymbolFontAlphabet{\mathtt}{mathttfont}

\edef\keptlbrack{\mathchar\the\mathcode[}
\edef\keptrbrack{\mathchar\the\mathcode]}
\begingroup
\catcode[=\active \catcode]=\active
\gdef[{%
\ifnum\mathgroup=\symmathttfont
\mathopen{\mathchar\numexpr"7000+$\relax} \else \keptlbrack \fi } \gdef]{% \ifnum\mathgroup=\symmathttfont \mathclose{\mathchar\numexpr"7000+$\relax}
\else
\keptrbrack
\fi
}
\endgroup
\AtBeginDocument{%
\mathcode$="8000 \mathcode$="8000
}

\begin{document}

$[\mathtt{x+y}]$

$\mathtt{[x+y]}$

$\displaystyle\left[\frac{a}{b}\right]$

\end{document}


I adapted my answer from here: How can I make every occurrence of + and - be replaced by a macro, but only in math mode?, borrowing liberally from here: How to set temporary math font?.

The result is that [ and ] get converted in math mode to the \texttt equivalent, but that \big and family as well as \left...\right are unaffected.

\documentclass{article}
\usepackage{lmodern}
\usepackage[T1]{fontenc}
\usepackage{amsmath}
%%%%% FROM https://tex.stackexchange.com/questions/385013/how-to-set-temporary-math-font/385068#385068
\usepackage{xpatch}
\DeclareMathVersion{ttmath}
\SetSymbolFont{letters}{ttmath}{OT1}{\ttdefault}{m}{n}
\xapptocmd{\ttfamily}{\mathversion{ttmath}}{}{}
%%%%% ADAPTED FROM
%%%%% https://tex.stackexchange.com/questions/392338/bracket-as-ordinary-symbol-in-math-mode/392350#392350
\makeatletter
% Commands that take the original meanings of the special characters
\let\lbr[
\let\rbr]
% Macro \setmathshorthands activates and defines the special characters
\begingroup
\catcode$=\active \catcode$=\active
\gdef[{\texttt{\lbr}}
\gdef]{\texttt{\rbr}}
\@ifdefinable{\setmathshorthands}{%
\xdef\setmathshorthands{%
\mathcode\number$="8000 % \mathcode\number$="8000 %
}%
}%
\endgroup
\makeatother
% Activate math shorthands in the math modes
\everymath{\setmathshorthands}
\everydisplay{\setmathshorthands}
%%%%%
\begin{document}
\ttfamily
\begin{equation*}
[x+y] \bigl[x\bigr] \left[\frac{a}{b}\right]
\end{equation*}
\begin{equation*}
\int_0^X f[x] dx
\end{equation*}
\end{document}