1

I would like to know if there is a way to include a MATLAB command within a sentence in LaTeX such that the command is in the MATLAB typeset format. I have seen examples using the matlab_prettifier package but that was for whole sections of code and no examples were given such that bits of code can be included within a normal sentence. I am describing a section of code for my thesis and I just need to include a single command within/in-line with one of my sentences.

JDS
  • 13
  • 1
    Welcome! In this case you can include that command/code-segment in a \verb|..| environment and mention line numbers in the MATLAB code for the audience to follow the demonstration. – AboAmmar Jan 20 '22 at 12:32

1 Answers1

1

You can write \lstinline[style=Matlab-editor]{<command>} to typeset commands inline with MATLAB editor style.

\documentclass{article}

\usepackage{matlab-prettifier}

\begin{document}

An example of an inline command is \lstinline[style=Matlab-editor]{d = rand(n,1)}. More text

\end{document}

inline matlab command

Somehow, this confuses the code highlighting in many editors (including this site). Therefore I would recommend to define a command

\newcommand{\lstinlineMatlab}[1]{\lstinline[style=Matlab-editor]{#1}}

and use \lstinlineMatlab{d = rand(n,1)} instead.

marv
  • 2,099
  • 1
  • 7
  • 26
  • Thanks! This works beautifully and even retains the code highlightings in overleaf :) – JDS Jan 20 '22 at 14:31
  • @JDS if this was helpfull, you can upvote and/or except the answer in the top left of this post (upwards arrow for upvote and checkmark for accept). – marv Jan 20 '22 at 15:13
  • Thanks for reminding me, I'm still a bit new to Stack Overflow – JDS Jan 21 '22 at 11:35