2

I'm making a set of notes with Tufte-latex in screen mode with a giant 8cm margin. There is plenty of room over there for code blocks, but minted doesn't work. There is an answer here but it doesn't actually put the code block in the margin...

\documentclass{tufte-book}

\usepackage{minted}
\usepackage{lipsum}

\geometry{screen,textheight=135mm,textwidth=10cm,marginparsep=1cm,marginparwidth=8cm}

\begin{document}

\section{Here's some code}

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

\marginnote{I want the code over here. But it doesn't work!}
\end{document}
2
  • 2
    you can't use verbatim commands such as minted in the argument of another command, you would be able to use mintedinput though if you put the python in another file Commented Aug 10 at 20:27
  • That'll work. Thanks David!
    – weymouth
    Commented Aug 11 at 7:59

2 Answers 2

1

You can't use verbatim commands such as minted in the argument of another command, you would be able to use \mintedinput though if you put the python in another file

0

Define a suitable environment:

\documentclass{tufte-book}

%\usepackage{tufte-latex} % <--- what's this?
\usepackage{minted}
\usepackage{lipsum}

\geometry{screen,textheight=135mm,textwidth=10cm,marginparsep=1cm,marginparwidth=8cm}

\newsavebox{\mintedbox}
\newenvironment{marginminted}{%
  \VerbatimEnvironment\begin{lrbox}{\mintedbox}%
  \begin{minipage}[t]{\marginparwidth}%
  \begin{minted}%
}{%
  \end{minted}\end{minipage}\end{lrbox}%
  \marginnote{\usebox{\mintedbox}}%
}


\begin{document}

\section{Here's some code}

Some text here%
\begin{marginminted}{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{marginminted}

\lipsum

\end{document}

enter image description here

1
  • Awesome. This works too! I think the \mintedinput solution is better for me since I have the code blocks in files, but this would be good if people didn't want to organize their code blocks like that.
    – weymouth
    Commented Aug 15 at 8:05

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .