8

Is it possible to use \listings or similar environment inside \marginnote or in margins in general?

\marginnote{
    \begin{lstlisting}
        test
    \end{lstlisting}
}

If not, what would be a workaround if I wanted to simply put coding notes in margins?

6

Here's my take with the listings package. The code below defines a new environment via \lstnewenvironment that saves the listing in a box and then typesets it in a \marginpar (or \marginnote if you like):

\documentclass{article}
\usepackage[marginparwidth=5cm,includemp,showframe]{geometry}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{listings,xcolor}

\newbox\marginlst
\newlength\marginlstoffset
% framerule + framesep + rulesep:
\setlength\marginlstoffset{\dimexpr2mm+3pt+2pt\relax}

\lstnewenvironment{marginlisting}
  {%
    \lstset{
      basicstyle = \small\ttfamily,
      frame = leftline ,
      framerule = 2mm ,
      rulecolor = \color{black!20},
      breaklines = true ,
      linewidth = \dimexpr\marginparwidth-\marginlstoffset\relax
    }%
    \setbox\marginlst\hbox\bgroup
  }
  {%
    \egroup
    % or \marginnote:
    \marginpar{\hspace*{\marginlstoffset}\unhbox\marginlst}%
  }

\usepackage{lipsum}

\begin{document}

Some initial words
\begin{marginlisting}
BEGIN
    FOR k IN c2(o.id) LOOP
        dbms_output.put_line('test');
    END LOOP;
END.
\end{marginlisting}
\lipsum[2]


\end{document}

enter image description here

  • This is perfect. – dwelle May 28 '13 at 17:37
4

With fancyvrb you can use an environment form:

\documentclass{article}
\usepackage[marginparwidth=5cm,includemp]{geometry}

\usepackage{marginnote,fancyvrb,xcolor}
\usepackage{lipsum}

\newsavebox{\marginlistingbox}
\newenvironment{marginlisting}
  {\fvset{tabsize=3,frame=leftline,framerule=2mm,rulecolor=\color{black!20}}%
   \setbox\marginlistingbox=\vtop\bgroup
     \hsize=\marginparwidth
     \topsep=0pt\partopsep=0pt
     \vspace*{-1.4\baselineskip}%
     \Verbatim}
  {\endVerbatim\egroup\marginnote{\leavevmode\box\marginlistingbox}}

\begin{document}
Some initial words%
\begin{marginlisting}
BEGIN
    FOR k IN c2(o.id) LOOP
        dbms_output.put_line('test');
    END LOOP;
END.
\end{marginlisting}
\lipsum[2]
\end{document}

enter image description here

I've little hope you can do it with listings.

1

So in the end I went with the fancyvrb solution as John Wickerson pointed out. It seems that for small code snippets to be used in margins it's quite useful (although there's no keywording feature that I know of)

% set options globally
\fvset{tabsize=3,frame=leftline,framerule=2mm,rulecolor=\color{black!20}} 

% define and save
\begin{SaveVerbatim}{name} 
BEGIN
    FOR k IN c2(o.id) LOOP
        dbms_output.put_line('test');
    END LOOP;
END.
\end{SaveVerbatim}

% use it
\marginnote{\UseVerbatim{name}}

... Lorem ipsum dolor sit amet, consectetur ...

gives:

Verbatim_result

fancyvrb at CTAN

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.