[Solved] Placing listings in page margins

Thomas Flinkow Asks: Placing listings in page margins

Goal​

My goal is to place listings in the margins. I want to use sidenotes for this, not snotez, because I also require other functionality present in the former, but not the latter.


What I tried​

I have absolutely no experience with LaTeX package development.

I tried to modify the code for the marginfigure from the sidenotes package documentation, which is

Code:
newsavebox{@sidenotes@marginfigurebox}
DeclareCaptionStyle{marginfigure}{font=footnotesize}
NewDocumentEnvironment{marginfigure} { o }
{
  begin{lrbox}{@sidenotes@marginfigurebox}
    begin{minipage}{marginparwidth}
      captionsetup{type=figure,style=marginfigure}
}
{
    end{minipage}%
  end{lrbox}%
  @sidenotes@placemarginal{#1}{usebox{@sidenotes@marginfigurebox}}
}

and I ended up with

Code:
newsavebox{@sidenotes@marginlistingbox}
newenvironment{marginlisting}
{
  begin{lrbox}{@sidenotes@marginlistingbox}
    begin{minipage}{marginparwidth}
      captionsetup{type=figure,style=marginlisting}
}
{
    end{minipage}%
  end{lrbox}%
  marginpar{usebox{@sidenotes@marginlistingbox}}
}

Result / Error message​

But when using this code I get the error message

Code:
A <box> was supposed to be here.
 
‪main.tex, 39‬
<to be read again> 
                   s
l.39 begin{marginlisting}
                          
I was expecting to see hbox or vbox or copy or box or
something like that. So you might find something missing in
your output. But keep trying; you can fix this later.

as well as this result:


enter image description here

The listing has the correct width, but it is not placed correctly (like the figure is). Further, there is sidenotes@marginlistingbox placed on the page multiple times in weird locations.


Question​

Could anyone please kindly point out what I did wrong and help me create a working marginlisting environment? Any help is gladly appreciated. Thank you in advance.


Complete MWE:​

Code:
documentclass[paper=a4]{scrartcl}
usepackage[utf8]{inputenc}

% page geometry
usepackage[inner=2cm, outer=7cm, marginparwidth=5cm, marginparsep=1cm]{geometry}
usepackage{showframe}

% content in margins
usepackage{marginnote}
usepackage{sidenotes}

% listings
usepackage[newfloat]{minted}
definecolor{bg}{RGB}{248,248,248}
setminted{bgcolor=bg,breaklines}

usepackage{caption}
usepackage{graphicx}

DeclareCaptionStyle{marginlisting}{font=footnotesize}

begin{document}

newsavebox{@sidenotes@marginlistingbox}
newenvironment{marginlisting}
{
  begin{lrbox}{@sidenotes@marginlistingbox}
    begin{minipage}{marginparwidth}
      captionsetup{type=figure,style=marginlisting}
}
{
    end{minipage}%
  end{lrbox}%
  marginpar{usebox{@sidenotes@marginlistingbox}}
}

section{Listing}

begin{marginlisting}
begin{minted}{bash}
# prerequisites
sudo apt-get install python3-pygments
end{minted}
caption{A listing in the margin}
end{marginlisting}

section{Figure}
begin{marginfigure}
    centering
    includegraphics[width=marginparwidth]{example-image-a}
    caption{A figure in the margin}
end{marginfigure}

end{document}

Ten-tools.com may not be responsible for the answers or solutions given to any question asked by the users. All Answers or responses are user generated answers and we do not have proof of its validity or correctness. Please vote for the answer that helped you in order to help others find out which is the most helpful answer. Questions labeled as solved may be solved or may not be solved depending on the type of question and the date posted for some posts may be scheduled to be deleted periodically. Do not hesitate to share your response here to help other visitors like you. Thank you, Ten-tools.