3

(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.

mjc
  • 745

2 Answers2

4

This is not a direct answer to your question, but (I hope) an answer to the larger problem you're trying to solve: presenting code in a homework assignment, paper or some such thing.

To do that, I think it's best to resort to a dedicated package that'll provide syntax highlighting, formatting etc., e.g. the listings package. The following example uses a random snippet of sample R code that Google found (I don't use R), and a style taken from this answer over on stackoverflow.com:

\documentclass{article}
\usepackage{listings}
\usepackage[dvipsnames]{xcolor}
\lstset{ 
  language=R,                     % the language of the code
  basicstyle=\tiny\ttfamily, % the size of the fonts that are used for the code
  numbers=left,                   % where to put the line-numbers
  numberstyle=\tiny\color{Blue},  % the style that is used for the line-numbers
  stepnumber=1,                   % the step between two line-numbers. If it is 1, each line
                                  % will be numbered
  numbersep=5pt,                  % how far the line-numbers are from the code
  backgroundcolor=\color{white},  % choose the background color. You must add \usepackage{color}
  showspaces=false,               % show spaces adding particular underscores
  showstringspaces=false,         % underline spaces within strings
  showtabs=false,                 % show tabs within strings adding particular underscores
  frame=single,                   % adds a frame around the code
  rulecolor=\color{black},        % if not set, the frame-color may be changed on line-breaks within not-black text (e.g. commens (green here))
  tabsize=2,                      % sets default tabsize to 2 spaces
  captionpos=b,                   % sets the caption-position to bottom
  breaklines=true,                % sets automatic line breaking
  breakatwhitespace=false,        % sets if automatic breaks should only happen at whitespace
  keywordstyle=\color{RoyalBlue},      % keyword style
  commentstyle=\color{YellowGreen},   % comment style
  stringstyle=\color{ForestGreen}      % string literal style
}
\begin{document}
\begin{lstlisting}
num = as.integer(readline(prompt="Enter a number: "))
factorial = 1
if(num < 0) {
    print("Sorry, factorial does not exist for negative numbers")
} else if(num == 0) {
    print("The factorial of 0 is 1")
} else {
    for(i in 1:num) {
        factorial = factorial * i
    }
    print(paste("The factorial of", num , "is", factorial))
}
\end{lstlisting}
\end{document}

enter image description here

The way the R code's presented is quite tweakable, and the invocation of \lstset is pretty well-documented, so it should serve as a useful starting point.

chsk
  • 3,667
4

If you don't need syntax coloring, you can use fancyvrb.

\documentclass{article}
\usepackage{fancyvrb,upquote}

\usepackage{lipsum} % for context

\DefineVerbatimEnvironment{Rcode}{Verbatim}{xleftmargin=\parindent,fontsize=\small}

\begin{document}

\lipsum[1][1-3] \begin{Rcode} num = as.integer(readline(prompt="Enter a number: ")) factorial = 1 if(num < 0) { print("Sorry, factorial does not exist for negative numbers") } else if(num == 0) { print("The factorial of 0 is 1") } else { for(i in 1:num) { factorial = factorial * i } print(paste("The factorial of", num , "is", factorial)) } \end{Rcode} \lipsum[2][3-6]

\end{document}

enter image description here

egreg
  • 1,121,712