# How can I use monospaced font inside a matrix (or tabular etc)?

I would like to display a matrix with characters using monospaced font (as in \tt and \mathtt), but I haven't found an easy way to do it. Something like this:

\begin{center}{\tt\left(
\begin{tabular}{cccc}
A & B & C & D\\
E & F & G & H\\
I & J & K & L
\end{tabular}\right)}.
\end{center}


But this won't work, because I used \left( and \right) outside math environment.

${\mathtt \begin{pmatrix} A & B & C & D\\ E & F & G & H\\ I & J & K & L \end{pmatrix} }$


This one won't work either:

! Missing \endcsname inserted.


I know I can do this by using {\mathtt A} & {\mathtt B}, and so on, for each matrix element, but that would be really a pain (I have more matrices).

Is there a simple way to do this?

-

I don't think it's a good idea to use the tabular environment inside a math group; instead, use the (very similar) array environment.

The following example code uses the newcolumntype command of the array package to create the t column type, which typesets its contents in monospaced font.

\documentclass{article}
\usepackage{array}
\newcolumntype{t}{>{\tt}c}
\begin{document}
$\left( \begin{array}{tttt} A & B & C & D\\ E & F & G & H\\ I & J & K & L \end{array} \right)$
\end{document}


Instead of \left( and \right) parentheses around the matrix, you could also use \left[ and \right] for square brackets, \left\{ and \right\} for curly braces, and \left\lvert and \right\rvert for straight lines, to name a few possibilities.

-
Never recommend the obsolete command \tt. Why shouldn't it be a good idea to use tabular in math mode? – egreg Nov 30 '11 at 18:17
I didn't know \tt was obsolete. What should I use instead? (I use \tt a lot in my documents!) – Jay Nov 30 '11 at 18:21
@Jay l2tabu is a good source for obsolete commands, including this. – Torbjørn T. Nov 30 '11 at 18:47
@egreg -- I know that \tt is deprecated. However, it's more straightforward to define the t column type using \tt for an array than with \ttfamily, which requires text mode. At least two good reasons for reserving use of the tabular and array environments for text and math modes, respectively: (i) the "standard" column types such as l, c, and r behave differently in text and math mode. Recall that the OP is trying to define a matrix and not some general two-dimensional array of text entries. (ii) the \tabcolsep and \arraycolsep length variables aren't the same. – Mico Nov 30 '11 at 18:58
@Mico Some classes don't even define \tt. – egreg Nov 30 '11 at 18:59
\usepackage{array}
\newcolumntype{t}{>{\ttfamily}c}

$\left(\begin{tabular}{tttt} A & B & C & D\\ E & F & G & H\\ I & J & K & L \end{tabular}\right)$


A tabular is quite good also in math mode. With \newcolumntype{t}{>{\ttfamily}c} you define a column made by centered cells typeset in typewriter type.

-
Thank you -- but I get ! LaTeX Error: Command \ttfamily invalid in math mode. with that... – Jay Nov 30 '11 at 18:28
@Jay Did you copy correctly? The inner environment is tabular, not array as in Mico's answer. – egreg Nov 30 '11 at 18:35
Ops... I changed one of the matrices to tabular, but there was another on left as array. Now it works! Thank you! – Jay Nov 30 '11 at 18:40
@Jay: Be aware that if you mix matrices typeset with array and tabular environments, the two will have different column widths -- unless you issue a command such as \setlength\tabcolsep\arraycolsep (a setting you probably don't want to have apply globally as it will otherwise affect "real", i.e., text-mode tabular environments as well). – Mico Nov 30 '11 at 19:19