如何将带有 minted 的代码放在边缘处?

如何将带有 minted 的代码放在边缘处?

在此处输入图片描述如何将带有 minted 包的代码放在页边距中(marginpar 不起作用)?我希望我的代码像文档一样放在页边距中,但我想使用 minted 包。谢谢。

\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

marginnote似乎不喜欢minted在里面有这样的代码。此外,代码太大,无法放入页面边距,除非你将文本宽度至少减少一半。

一种解决方法是将wrapfigure和组合minipage如下,这样您就不必牺牲整个页面的文档宽度,而只需牺牲代码出现的位置。

在此处输入图片描述

\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}

相关内容