add tag
topnush
How can I add a right-aligned comment in listings? Take this code:

```
\documentclass[xcolor={rgb}]{beamer}
\beamertemplatenavigationsymbolsempty
\setbeamersize{text margin left=10mm,text margin right=5mm} 
\setbeamertemplate{frametitle}[default][center]
\usepackage[T1]{fontenc}
\usepackage{bm}
\usepackage[many]{tcolorbox}
\usepackage{listings}
\lstdefinestyle{duckstyle}{%
    moredelim=[is][\color{red}]{|}{|},
    mathescape=true,
    escapechar=@,
    basicstyle=\ttfamily,
    columns=fullflexible
}
\lstset{style=duckstyle}
\tcbuselibrary{skins}
\tcbset{
	colback=white
}
\begin{document}
\begin{frame}[ fragile]
\begin{tcolorbox}[rounded corners, top=0pt, left=5pt,right=5pt, colback=blue!5!white, text height=7.43cm]
\begin{lstlisting}[basicstyle=\linespread{0.9}\ttfamily, mathescape, name=sparserecovery, basewidth = {.3em}]
return $\bm{f} = \bm{0}$
\end{lstlisting}
\end{tcolorbox}
\end{frame}

\end{document}
```

I would like to add "(A comment)" as a right-aligned comment on the first line.  Maybe it should be a different color to make it clear it is a comment I am not sure.

I am happy to use tcblisting instead if that makes things easier.
Top Answer
user 3.14159
I am assuming that the preamble is similar to what you had before. As before, I am using the `listings` library of `tcolorbox`. Here are quick illustrations of two options:
1. A comment on the side using the `listing side comment` key from `tcolorbox`.
2. Comments using the `listings` sublibrary of the `tikzmark` library.

These are just sketches but may go in the right direction. An important point about these constructions is that you do not need to place anything in the source codes that you are listing. That is, you can copy or input the codes 1:1 without destroying them by placing something inside. This is one of the main virtues of the `listings` sublibrary of the `tikzmark` library. If this is not important, it can be that other methods are simpler.

```
\documentclass{beamer}
\usepackage{bm}
\usepackage[many,listings]{tcolorbox}
\lstdefinestyle{duckstyle}{%
    moredelim=[is][\color{red}]{|}{|},
    mathescape=true,
    escapechar=@,
    basicstyle=\ttfamily,
    columns=fullflexible
}
\lstset{style=duckstyle}
\tcbuselibrary{skins}
\tcbset{
	colback=white
}
% for the second option
\usepackage{tikz}
\usetikzlibrary{tikzmark}
\usetikzmarklibrary{listings}
\begin{document}

\begin{frame}[fragile]
\frametitle{\texttt{listing side comment}}
\begin{tcblisting}{boxsep=0pt,top=0pt,bottom=0pt,left=5pt,right=5pt,colback=blue!5!white,
listing only,
listing options={mathescape, name=countsketchshort, basewidth = {.3em}},
listing side comment,righthand width=1.5cm,
listing and comment,comment={{}pft}}
return $\bm{f} = \bm{0}$ 

$1$-sparse $(j, c)$
set $\ell = \ell + c$
set $z = z + cj$
\end{tcblisting}
\end{frame}

\begin{frame}[fragile]
\frametitle{\texttt{tikzmark}}
\begin{tikzpicture}[overlay,remember picture]
\path ([xshift=2cm]current page.center) coordinate (M);
\iftikzmark{line-code-1-end}{%
\path (pic cs:line-code-1-end) coordinate (aux) 
(aux-|M) node[above right,inner sep=0pt,text depth=0pt] {some text};}{%
\typeout{line 1 not found}}
\iftikzmark{line-code-2-end}{%
\path (pic cs:line-code-2-end) coordinate (aux) 
(aux-|M) node[above right,inner sep=0pt,text depth=0pt] {more text};}{%
\typeout{line 2 not found}}
\end{tikzpicture}%
\begin{tcblisting}{top=0pt,bottom=0pt,left=5pt,right=5pt,colback=blue!5!white,
listing only,hbox,
listing options={mathescape,basewidth = {.3em},name=code}
}
return $\bm{f} = \bm{0}$
return $\hat{f}_{a_i} =$ median{$g_j(a_i) C[j, h_j(a_i)]$
\end{tcblisting}
\end{frame}

\begin{frame}[fragile]
\frametitle{\texttt{tikzmark} inside}
\begin{tcblisting}{top=0pt,bottom=0pt,left=5pt,right=5pt,colback=blue!5!white,
listing only,
listing options={mathescape, basewidth = {.3em},name=code2}
}
return $\bm{f} = \bm{0}$ 

$1$-sparse $(j, c)$
set $\ell = \ell + c$
set $z = z + cj$
\end{tcblisting}
\begin{tikzpicture}[overlay,remember picture]
\path ([xshift=2cm]current page.center) coordinate (M);
\iftikzmark{line-code2-3-end}{%
\path (pic cs:line-code2-3-end) coordinate (aux) 
(aux-|M) node[above right,inner sep=0pt,text depth=0pt] {explanation};
}{%
\typeout{line 3 not found}}
\iftikzmark{line-code2-4-end}{%
\path (pic cs:line-code2-4-end) coordinate (aux) 
(aux-|M) node[above right,inner sep=0pt,text depth=0pt] {more stuff};}{%
\typeout{line 4 not found}}
\end{tikzpicture}%
\end{frame}

\end{document}
```

![ani.gif](/image?hash=d9661c93b944d2ff13098c0c14794893048b041cac596cc69833ce2f57bf40dc)
Answer #2
Skillmon
My comment to use `\hfill` inside of an escape turned into an answer. Since you're escaping to LaTeX you can use anything LaTeX provides to further format your comment (*e.g.*, you could use `\makebox[<length>][l]{(comment)}` to get the comments aligned on the left below each other (though the correct value of `<length>` would require some fiddling).

```
\documentclass[xcolor={rgb}]{beamer}
\beamertemplatenavigationsymbolsempty
\setbeamersize{text margin left=10mm,text margin right=5mm} 
\setbeamertemplate{frametitle}[default][center]
\usepackage[T1]{fontenc}
\usepackage{bm}
\usepackage[many]{tcolorbox}
\usepackage{listings}
\lstdefinestyle{duckstyle}{%
    moredelim=[is][\color{red}]{|}{|},
    mathescape=true,
    escapechar=@,
    basicstyle=\ttfamily,
    columns=fullflexible
}
\lstset{style=duckstyle}
\tcbuselibrary{skins}
\tcbset{
	colback=white
}
\begin{document}
\begin{frame}[ fragile]
\begin{tcolorbox}[rounded corners, top=0pt, left=5pt,right=5pt, colback=blue!5!white, text height=7.43cm]
\begin{lstlisting}[basicstyle=\linespread{0.9}\ttfamily, mathescape, name=sparserecovery, basewidth = {.3em}]
complicated code@\hfill (a comment)@
return $\bm{f} = \bm{0}$@\hfill (another comment)@
\end{lstlisting}
\end{tcolorbox}
\end{frame}

\end{document}
```

![rightcomment-1.png](/image?hash=43829b6836121e12bf0252612c12e1e97970e46ccecada4144ef5be0269dd065)

And a version using `\makebox` as previously suggested to align the comments:

```
\documentclass[xcolor={rgb}]{beamer}
\beamertemplatenavigationsymbolsempty
\setbeamersize{text margin left=10mm,text margin right=5mm} 
\setbeamertemplate{frametitle}[default][center]
\usepackage[T1]{fontenc}
\usepackage{bm}
\usepackage[many]{tcolorbox}
\usepackage{listings}
\lstdefinestyle{duckstyle}{%
    moredelim=[is][\color{red}]{|}{|},
    mathescape=true,
    escapechar=@,
    basicstyle=\ttfamily,
    columns=fullflexible
}
\lstset{style=duckstyle}
\tcbuselibrary{skins}
\tcbset{
	colback=white
}
\begin{document}
\begin{frame}[ fragile]
\begin{tcolorbox}[rounded corners, top=0pt, left=5pt,right=5pt, colback=blue!5!white, text height=7.43cm]
\begin{lstlisting}[basicstyle=\linespread{0.9}\ttfamily, mathescape, name=sparserecovery, basewidth = {.3em}]
complicated code@\hfill\makebox[4cm][l]{(a comment)}@
return $\bm{f} = \bm{0}$@\hfill\makebox[4cm][l]{(another comment)}@
\end{lstlisting}
\end{tcolorbox}
\end{frame}

\end{document}
```

![rightcomment-1.png](/image?hash=4ec39eacce1fdf5e646a1465ff89f134a7daf0bedfd961c58404a3ddf202c7f5)

Enter question or answer id or url (and optionally further answer ids/urls from the same question) from

Separate each id/url with a space. No need to list your own answers; they will be imported automatically.