# Effective Methods For Typesetting Matlab Code In Latex Documents

## Importing and Formatting MATLAB Code

When including MATLAB code in a LaTeX document, it is important to properly format the code to make it more readable. The `matlab-prettifier`

is a useful LaTeX package for importing and syntax highlighting MATLAB code.

To import an entire MATLAB script file into LaTeX, the `\lstinputlisting`

command can be used. The `matlab-prettifier`

package will automatically detect MATLAB syntax and highlight keywords, comments, strings, etc. in different colors. Additionally, line numbers can be enabled using the `numbers=left`

option.

```
\usepackage{matlab-prettifier}
\begin{lstlisting}[language=Matlab,
numbers=left]
\lstinputlisting{myscript.m}
\end{lstlisting}
```

The font size, line spacing, margins, and other typographical elements can be customized by passing additional options to `\lstlisting`

. Generally, a smaller monospaced font with 1.0 or 1.1 line spacing provides the best results for readability.

## Inserting Figures and Plots

MATLAB's plotting functions can generate high-quality graphics that may need to be included alongside code listings or referenced in text. The preferred approach is to export plots as external image files.

To generate plots, the `print`

or `export_graphics`

functions can be used to save plots in PDF, PNG, JPEG, etc. formats. The graphic image files can then be included in the LaTeX document using the standard `\includegraphics`

macro.

```
\begin{figure}[ht!]
\centering
\includegraphics[width=0.8\textwidth]{plotfile}
\caption{MATLAB plot caption}
\end{figure}
```

Make sure to set appropriate image widths and positioning captions or labels for plot images inserted. Using the `figure`

environment with `[ht]`

placement specifiers allows LaTeX more flexibility in placing larger images.

## Displaying Code Output and Results

When showing output generated from MATLAB code, the `\verbatim`

environment can be highly effective for preserving all whitespace, text formatting, line breaks, etc.

By default, `\verbatim`

uses a monospaced font and disables LaTeX special characters. This allows output text to be copied directly from the MATLAB command prompt without worrying about alignment or font issues.

```
\begin{verbatim}
>> format long
>> myvar = 3.14159265358979
\end{verbatim}
```

Margin sizes may need to be adjusted using `\begin{verbatim*}`

to handle extra width from long text lines in output. Referring to output values in the main text is also common and can be formatted nicely by escaping out of verbatim mode.

## Troubleshooting Common Issues

Getting MATLAB and LaTeX integration running smoothly can involve some debugging of packages, file paths, compilers, etc. Here are solutions to some common pitfalls.

**Missing packages** - If unusual LaTeX errors occur when importing MATLAB code, ensure matlab-prettifier and its token library dependencies are installed on your system and referenced correctly.

**Path errors** - Including graphic image files relies heavily on relative file paths. Double check that image file locations are specified correctly relative to the main LaTeX .tex document.

**Formatting inconsistencies** - With multiple syntax elements importing code, output, and images, font sizes, margins, and text flow may not always align. Careful troubleshooting of spacing options is key.

## Example MATLAB Code Listings

Below are examples demonstrating effective strategies for incorporating MATLAB code, plots, and output in LaTeX documents:

### Importing .m File and Highlighting Syntax

```
\usepackage{matlab-prettifier}
\begin{lstlisting}[language=Matlab,
numbers=left,
numberstyle=\tiny,
basicstyle=\ttfamily\footnotesize]
\lstinputlisting[linerange={1-10}]{surfaceplot.m}
\end{lstlisting}
```

- Imports lines 1-10 of surfaceplot.m
- Applies syntax highlighting for MATLAB code
- Shows line numbers on left in tiny font
- Uses smaller monospaced font for code

### Inserting Plot Image File with Caption

```
\begin{figure}[ht]
\centering
\includegraphics[width=0.8\textwidth]{surfaceplot}
\caption{Surface plot for 3D quadratic function}
\end{figure}
```

- Places surface plot image file with 80% width
- Allows LaTeX to position image in optimal location
- Adds descriptive caption below figure

### Showing Output in Verbatim Environment

```
\begin{verbatim*}
>> format long
>> myvar = 3.14159265358979
>> disp(myvar)
3.14159265358979
\end{verbatim*}
The value of \texttt{myvar} shown in the output is the first 8 digits of $\pi$.
```

- Preserves whitespace and text formatting
- Handles extra width from long output line
- Escapes verbatim to reference \texttt{myvar} in text