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.
Best Answer
Although @cfr have already made a answer. I think I still want post a another solution for you. As proposed by cfr's answer, the second option is the vertical space from the insert point you want the margin note to be located. This is working fine as you could manually adjust the location of the margin note. However, you need to calculate the distance and put the margin note at suitable location each time.
There is another solution, if you use the default
\marginpar{}
command. By setting aescapechar
for the listing env. And put the\marginpar{}
between the escape character inside the listing env. In this way, listing env will still process the command as normal way instead is displaying them as verb contents. The margin note will be generated at the exact line which you insert the command.Here is the example showing difference of two solutions: