1

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.

enter image description here

So I'd like it to look like

enter image description here

Or in the middle:

enter image description here

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

enter image description here

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:

enter image description here

So this solution does not seem to work.

Nasser
  • 20,220
  • You have aboveskip=12pt so 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:51
  • The second option is the vertical space from the insert point you want the margin note to be located. – Tom Nov 20 '23 at 04:51
  • Any reason you must use a marginnote package? You could set a escapechar for the listing env and using \marginpar{} to put margin note beside the listing env wherever you like. – Tom Nov 20 '23 at 04:54
  • @Nasser I post a answer. – Tom Nov 20 '23 at 05:21
  • I think you should accept @Tom's answer for the reason I note in a comment. – cfr Nov 20 '23 at 16:38

2 Answers2

3

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 a escapechar 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:

\documentclass{article}
\usepackage{listings}
\usepackage{fancybox,fancyvrb,xcolor}
\usepackage{marginnote}
\usepackage{lipsum}

\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, escapechar=@, %everything in between @ and @ will be processed as a normal way }

\lstnewenvironment{TEXTinline}{% \lstset{style=TEXT}}{}

\begin{document} \reversemarginpar %Use \normalmarginpar to switch back. \lipsum[1] \marginnote{Command}[1.3cm] \begin{TEXTinline} This is my long command which can be very very very long command used to process the file \end{TEXTinline}

\lipsum[1]

\begin{TEXTinline} This is another of my long commands which can be very very very @\marginpar{Command}@long command used to process the file \end{TEXTinline}

\end{document}

enter image description here

Tom
  • 7,318
  • 4
  • 21
  • You might want to point out that a big advantage of your solution is that the margin note is less likely to end up on the wrong page or no page at all because it is tied to the environment itself. – cfr Nov 20 '23 at 16:36
  • @cfr Thank you for this statement. – Tom Nov 20 '23 at 19:07
2

I didn't suggest moving the listing up. I suggested moving the note down.

\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}[12pt] \begin{TEXTinline} This is my long command which can be very very very long command used to process the file \end{TEXTinline}

\marginnote{Command}[12pt] \begin{TEXTinline} This is another of my long commands used to process the file \end{TEXTinline}

\end{document}

aligned marginal notes

With added Kant:

shown in relation to surrounding text

cfr
  • 198,882
  • Thanks. I misunderstood you. – Nasser Nov 20 '23 at 04:27
  • @Nasser No problem. I probably could have been clearer. I mentioned the aboveskip to explain what you'd need to compensate for. – cfr Nov 20 '23 at 04:29
  • 1
    FYI, your solution works fine. But I found issue related to using book style. Since this is not covered in my MWE, I will make new question. I do not like to ask follow up on same question when I make changes to my MWE. – Nasser Nov 20 '23 at 05:47