3

enter image description herehow 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}
2

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.

enter image description here

\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}
  • 1
    Thank you Superuser27 for this answer. – F M Mar 27 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 at 9:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.