6

I've tried to add margin notes for listings (PS: footnotes work fine), but when line numbering is on, the margin notes disturb the numbers:

\documentclass{article}

\usepackage{listings}
\usepackage{marginnote}

\begin{document}

\lstset{language=C++,numbers=left,escapeinside={(*@}{@*)}}

\begin{lstlisting}
for (int i = 0; i < 10; i++)
{
    cout << i << endl;
}
\end{lstlisting}

\begin{lstlisting}
for (int i = 0; i < 10; i++)
{
    cout << i << endl;(*@\marginnote{margin note}@*)
}
\end{lstlisting}

%% I've tried to wrap \marginnote with boxes, but could't fix it.

\begin{lstlisting}
for (int i = 0; i < 10; i++)
{
    cout << i << endl;(*@\vbox{\marginnote{margin note}}@*)
}
\end{lstlisting}

\end{document}

result

5

It the package marginnote is not obligatory, \marginpar does the work:

\documentclass{article}

\usepackage{listings}
%\usepackage{marginnote}

\begin{document}

\lstset{language=C++,numbers=left,escapeinside={(*@}{@*)}}

\begin{lstlisting}
for (int i = 0; i < 10; i++)
{
    cout << i << endl;
}
\end{lstlisting}

\begin{lstlisting}
for (int i = 0; i < 10; i++)
{
 cout << i << endl;(*@\marginpar{margin note}@*)
}
\end{lstlisting}

%% I've tried to wrap \marginnote with boxes, but could't fix it.

\begin{lstlisting}
for (int i = 0; i < 10; i++)
{
cout << i << endl;(*@\marginpar{margin note}@*)
}
\end{lstlisting}

\end{document}

enter image description here

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.