# How to put code with minted in the margin?

how to put code with the package minted in the margin (marginpar did not work)? I want my code in the marge like the document but I want use the package minted. Thank you.

\documentclass{article}
\usepackage{geometry}   % for margin note
\usepackage{marginnote} % for margin note
\usepackage{minted}     % for highlighted code

\begin{document}
\marginnote{  %this should appear in the margin (doesn't work)
\begin{minted}{python}
import numpy as np

def incmatrix(genl1,genl2):
m = len(genl1)
n = len(genl2)
M = None #to become the incidence matrix
VT = np.zeros((n*m,1), int)  #dummy variable

#compute the bitwise xor matrix
M1 = bitxormatrix(genl1)
M2 = np.triu(bitxormatrix(genl2),1)
\end{minted}}

\begin{minted}{python} % this correctly appears in the document
import numpy as np

def incmatrix(genl1,genl2):
m = len(genl1)
n = len(genl2)
M = None #to become the incidence matrix
VT = np.zeros((n*m,1), int)  #dummy variable

#compute the bitwise xor matrix
M1 = bitxormatrix(genl1)
M2 = np.triu(bitxormatrix(genl2),1)
\end{minted}
\end{document}


## 1 Answer

marginnotes don't seem to like having code like minted inside. Also the code is way too large to fit in the page's margin, unless you reduce the text width by at least half.

A workaround could be to combine wrapfigure and minipages as follows, so you don't have to sacrifice the document's width over the whole page, but just where the code appears.

\documentclass{article}
\usepackage{wrapfig}
\usepackage{minted}
\usepackage{lipsum}

\begin{document}
\begin{wrapfigure}{r}{0.5\linewidth}
\hspace{-2.8cm}
\begin{minipage}[h]{\linewidth}
\begin{minted}{python}
import numpy as np

def incmatrix(genl1,genl2):
m = len(genl1)
n = len(genl2)
M = None #to become the incidence matrix
VT = np.zeros((n*m,1), int)  #dummy variable

#compute the bitwise xor matrix
M1 = bitxormatrix(genl1)
M2 = np.triu(bitxormatrix(genl2),1)
\end{minted}
\end{minipage}
\end{wrapfigure}
\lipsum[1-3]
\end{document}

• Thank you Superuser27 for this answer. – F M Mar 27 '19 at 18:19
• @FM you're welcome :) remember to mark an answer as "accepted" if the solution works for you, by clicking on the green check mark – Superuser27 Mar 28 '19 at 9:02