2

This post, defines a comment command which can be used via escape strings. Is there a way to get a defined comment string to be reformatted in the pdf output somehow?

For example would it be possible to have something like...

\documentclass{article}
\usepackage{listings}

\lstset{basicstyle=\small\sffamily, numbers=none, frame=tb, columns=fullflexible, comment=[l]{#}, showstringspaces=false, escapechar=&% char to escape out of listings and back to LaTeX }

\begin{document} \begin{lstlisting} Hello # Comment 1 World # Comment 2 \end{lstlisting} \end{document}

do the same thing as...

\documentclass{article}
\usepackage{listings}

\newcommand*{\Comment}[1]{\hfill\makebox[3.0cm][l]{$\triangleright$ #1}}% \lstset{basicstyle=\small\sffamily, numbers=none, frame=tb, columns=fullflexible, showstringspaces=false, escapechar=&% char to escape out of listings and back to LaTeX }

\begin{document} \begin{lstlisting} Hello &\Comment{Comment 1}& World &\Comment{Comment 2}& \end{lstlisting} \end{document}

by somehow defining the comment string & making commentstyle use the \Comment{} command?

user41177
  • 375

1 Answers1

3

A first solution is to use the option moredelim=**[il][\hfill$\triangleright$ ]{\#}. Due to the \hfill, the 2 occurences of Comment are not precisely below each other if the numbers do not have the same number of digits.

enter image description here

\documentclass{article}
\usepackage[paperwidth=8cm,paperheight=2cm,left=0.5cm,right=0.5cm]{geometry}
\usepackage{listings}
\lstset{
  basicstyle=\small\sffamily,
  frame=tb,
  columns=fullflexible,
  moredelim=**[il][\hfill$\triangleright$ ]{\#}
}
\begin{document}
\begin{lstlisting}
  Hello  # Comment 9
  World  # Comment 10
\end{lstlisting}
\end{document}

A second solution is to use the options

escapeinside={\#}{\^^M},
escapebegin={\hfill\begin{minipage}[t]{3cm}$\triangleright$},
escapeend={\end{minipage}}

so that a space of 3cm is reserved.

enter image description here

\documentclass{article}
\usepackage[paperwidth=8cm,paperheight=2cm,left=0.5cm,right=0.5cm]{geometry}
\usepackage{listings}
\lstset{
  basicstyle=\small\sffamily,
  frame=tb,
  columns=fullflexible,
  escapeinside={\#}{\^^M},
  escapebegin={\hfill\begin{minipage}[t]{3cm}$\triangleright$},
  escapeend={\end{minipage}}
}
\begin{document}
\begin{lstlisting}
  Hello  # Comment 9
  World  # Comment 10
\end{lstlisting}
\end{document}
matexmatics
  • 4,819
  • @samcarter_is_at_topanswers.xyz Thank you! The most essential part is that \begin{minipage} can be placed in escapebegin and \end{minipage} in escapeend. – matexmatics Jan 02 '24 at 15:49
  • I was more impressed by the idea to use the end of the line as stop of the escape. I was toying around with possible solutions, but all of them required an additional # at the end of the line :) – samcarter_is_at_topanswers.xyz Jan 02 '24 at 15:52
  • @matexmatics Great solution. – yannisl Jan 02 '24 at 16:03
  • Thank you so much! This is very cool. May I ask which solution you recommend between the two? – user41177 Jan 03 '24 at 04:55
  • @user41177 As mentioned in the answer: in the first solution the 2 occurences of Comment are not precisely below each other if the numbers do not have the same number of digits. In the first solution it is possible to write for example Comment 10$. This is not possible in the second solution since it escapes to LaTeX so then this should be inputted as Comment 10\$. Which solution is best depends on the situation. – matexmatics Jan 03 '24 at 11:09