# Escaping the “^” character causes formatting error

Escaping "^" after \tt is causing formatting error in the following code. All words after \^{} is being affected by \tt.

Code:

\item Use the {\tt linspace} command to create a vector $\mathbf{x} \in \mathbb{R}^{100}$ containing 100 equally spaced points between 0 and 10 (inclusive). Use this vector to create the Vandermonde matrix $\mathbf{D} \in \mathbb{R}^{100 \times 4}$ with rows $\begin{bmatrix} 1 & x_i & x_i^2 & x_i^3 \end{bmatrix}$ for $i = 1,\dots,100$. (Hint: While using {\tt x\^{}n} will result in an error, using {\tt x.\^{}n} will return a vector where each element is raised to the $n$th power.) Save the matrix $\mathbf{D}$ as {\tt A4.dat}.


Current output:

Is there a way to resolve this issue?

• Well, now you should not use \tt at all. In this case, I think \verb fits best – JouleV Apr 26 at 4:44
• However, your code works fine for me – JouleV Apr 26 at 4:47
• Independent of your question, you might be interested in this package: matlab-prettifier. – Henri Menke Apr 26 at 5:06

Your code works file for me. However,

1. you should not use the deprecated \tt. Such commands are some of the most deprecated commands in LaTeX.

2. \verb is for that purpose. You can use something as simple as \verb|a^b| to get a^b instead of \texttt{a\^{}b}; or \verb|\cmd| to get \cmd instead of \texttt{\textbackslash{}cmd}.

Code:

\documentclass{article}
\usepackage{mathtools}
\usepackage{amssymb}
\usepackage[shortlabels]{enumitem}
\begin{document}
\begin{enumerate}[(a)]
\item Use the \verb|linspace| command to create a vector $\mathbf{x} \in \mathbb{R}^{100}$ containing 100 equally spaced points between 0
and 10 (inclusive). Use this vector to create the Vandermonde matrix
$\mathbf{D} \in \mathbb{R}^{100 \times 4}$ with rows $\begin{bmatrix} 1 & x_i & x_i^2 & x_i^3 \end{bmatrix}$ for $i = 1,\dots,100$. (Hint:
While using \verb|x^n| will result in an error, using \verb|x.^n| will
return a vector where each element is raised to the $n$-th power.)
Save the matrix $\mathbf{D}$ as \verb|A4.dat|.
\end{enumerate}
\end{document}