(Original title: "Set font in an align environment".)
Problem
I want to make an align environment with \texttt (or, more generally, any font) everywhere by default.
Motivation
I'm presenting R code in the appendix of a homework report for a statistics module of a second-year university course. At present I'm writing like this:
\begin{align*}
& \texttt{some code} \\
& \texttt{some more code} \\
& \texttt{yet still more further code} \\
& \texttt{this gets pretty inconvenient over many lines, especially} \\
& \hspace{1em} \texttt{if I have to break a line of code over two lines.}
\end{align*}
Attempts so far
This post shows how to define a custom font environment, but I can't figure out how to apply it to an align environment.
\documentclass{article}
\usepackage{amsmath}
\newenvironment{myfont}{\fontfamily{lmtt}\selectfont}{\par} % from the post
\newenvironment{mycodeA}{\fontfamily{lmtt}\selectfont\begin{align*}}{\end{align*}} % what I want, attempt 1
\newenvironment{mycodeB}{\begin{align*}\fontfamily{lmtt}\selectfont}{\end{align*}} % what I want, attempt 2
\begin{document}
\begin{myfont}hi\end{myfont} % works
\begin{mycodeA}hi\end{mycodeA} % doesn't work
\begin{mycodeB}hi\end{mycodeB} % doesn't work
\end{document}
Solution
My particular problem is solved by
\newenvironment{mycode}{\begin{quote}\fontfamily{lmtt}\selectfont}{\end{quote}}
based on a suggestion from @egreg. However, @chsk has come up with a much more powerful solution to the more general case. I've renamed the post to reflect the change in emphasis.


alignshould be necessary? – egreg Apr 02 '21 at 21:13listingspackage would be a better choice. – chsk Apr 02 '21 at 21:21align? It looktabularto me. Or a variation ofquote– egreg Apr 02 '21 at 21:23alignfor this at all. – David Carlisle Apr 02 '21 at 21:57