I'd like to put a small text on left margin of page that have small description of code/command listing, where on the main page there is code listing using listing package.
But the description text I put on left margin is always higher than when the listing starts and not aligned.
I'd like this left margin text to be at same vertical level, or in the middle. Now it does not look good. This is how it is now. Any other solution that does not use this package is fine also.
So I'd like it to look like
Or in the middle:
Either way is fine
MWE below. TL 2023 with lualatex compiler.
\documentclass{article}
\usepackage{listings}
\usepackage{fancybox,fancyvrb,xcolor}
\usepackage{marginnote}
\definecolor{bg}{RGB}{255,255,226}
\lstdefinestyle{TEXT}{%
basicstyle=\ttfamily\normalsize,
breaklines=false,
columns=fullflexible,
keepspaces=true,
backgroundcolor=\color{bg},
rulecolor=\color{gray},
language=,
frame=single,
frameround=tttt,
aboveskip=12pt,belowskip=6pt
}
\lstnewenvironment{TEXTinline}{%
\lstset{style=TEXT}}{}
\begin{document}
\reversemarginpar
%Use \normalmarginpar to switch back.
\marginnote{Command}[0cm]
\begin{TEXTinline}
This is my long command
which can be very very very
long command
used to process the file
\end{TEXTinline}
\marginnote{Command}[0cm]
\begin{TEXTinline}
This is another of my long commands
used to process the file
\end{TEXTinline}
\end{document}
Putting the marginnote after the listings did not help.
\begin{TEXTinline}
This is another of my long commands
used to process the file
\end{TEXTinline}
\marginnote{Command}[0cm] %now it is below the listings
Update
I had aboveskip=12pt in order to automatically have vertical space above listings so I do not have to manually remember to do that. Here is a version showing what I mean when I changed it to aboveskip=0
\documentclass{article}
\usepackage{listings}% added lstautogobble oct 30,2020
\usepackage{fancybox,fancyvrb,xcolor}
\usepackage{marginnote}
\definecolor{bg}{RGB}{255,255,226}
\lstdefinestyle{TEXT}{%
basicstyle=\ttfamily\normalsize,
breaklines=false,
columns=fullflexible,
keepspaces=true,
backgroundcolor=\color{bg},
rulecolor=\color{gray},
language=,
frame=single,
frameround=tttt,
aboveskip=0pt,belowskip=6pt
}
\lstnewenvironment{TEXTinline}{%
\lstset{style=TEXT}}{}
\begin{document}
\reversemarginpar
This is my code
\marginnote{Command}[0cm]
\begin{TEXTinline}
This is my long command
which can be very very very
long command
used to process the file
\end{TEXTinline}
\end{document}
Gives
So now I have to fix the above so that vertical space between main text and listing is correct. But this might be easier to fix. But it means I have to remember to add explicit vertical space each time I use listing. Before, this was done automatically.
But now I know the problem, so I just need to remember to add vertical space manually each time. Something like this?
This is my code
\vspace{12pt}
%
\marginnote{Command}[0cm]
\begin{TEXTinline}
This is my long command
which can be very very very
long command
used to process the file
\end{TEXTinline}
But the problem now comes back again:
So this solution does not seem to work.








aboveskip=12ptso you need to move it down. But in some cases, you'll probably end up with the margin note on the wrong page or on no page. – cfr Nov 20 '23 at 03:51marginnotepackage? You could set aescapecharfor the listing env and using\marginpar{}to put margin note beside the listing env wherever you like. – Tom Nov 20 '23 at 04:54