[Tex/LaTex] Inline python code in latex

codeinline()

I am trying to add some inline python code into my report with color in an box like following:

enter image description here

As you can see "matrix text is red coloured embedded in a box"
I can add inline colour code using the following method:

CVXOPT extends the built-in Python objects with two matrix objects: a {\color{red}\texttt{matrix}} object for dense

Which produces the following output:
enter image description here

It is almost perfect except a box around the code. How I can have a box around the code?

Best Answer

You can simply put it inside an \fbox{...}:

\documentclass[border=5pt]{standalone}
\usepackage{xcolor}

\newcommand{\pyobject}[1]{\fbox{\color{red}{\texttt{#1}}}}

\begin{document}

CVXOPT extends the built-in Python objects with two matrix objects: a \pyobject{matrix} object for dense

\end{document}

Output, square corners

Or, if you want rounded corners, use \ovalbox{...} from the fancybox package:

\documentclass[border=5pt]{standalone}
\usepackage{xcolor,fancybox}

\newcommand{\pyobject}[1]{\ovalbox{\color{red}{\texttt{#1}}}}

\begin{document}

CVXOPT extends the built-in Python objects with two matrix objects: a \pyobject{matrix} object for dense

\end{document}

Output, rounded corners