topnush
I want to save a listings entry to be used in various places. My non-working code is:
```
\documentclass{beamer}
\beamertemplatenavigationsymbolsempty
\setbeamertemplate{frametitle}[default][center]
\usepackage[many]{tcolorbox}
\usepackage{listings}
\lstdefinestyle{duckstyle}{%
moredelim=[is][\color{red}]{|}{|},
mathescape=true,
escapechar=@,
basicstyle=\ttfamily,
columns=fullflexible
}
\lstset{style=duckstyle}
\newsavebox{\boxname}
\savebox{\boxname}{
\begin{tcolorbox}[top=0pt, left=5pt,right=5pt, colback=blue!5!white, text height=5.5cm]
\begin{lstlisting}[ mathescape, name=tidemark, basewidth = {.3em}]
test
\end{lstlisting}
\end{tcolorbox}
}
\newcommand{\Proc}[1]{\textsc{#1}}
\begin{document}
\begin{frame}[fragile, t]{}
\usebox{\boxname}
\end{frame}
\end{document}
```
This doesn't render. What am I doing wrong?
Top Answer
user 3.14159
With `xsavebox` there is no problem.
```
\documentclass{beamer}
\beamertemplatenavigationsymbolsempty
\setbeamertemplate{frametitle}[default][center]
\usepackage[many]{tcolorbox}
\usepackage{listings}
\usepackage{xsavebox}
\lstdefinestyle{duckstyle}{%
moredelim=[is][\color{red}]{|}{|},
mathescape=true,
escapechar=@,
basicstyle=\ttfamily,
columns=fullflexible
}
\lstset{style=duckstyle}
\begin{document}
\begin{xlrbox}{boxname}
\begin{tcolorbox}[top=0pt, left=5pt,right=5pt, colback=blue!5!white, text height=5.5cm]
\begin{lstlisting}[ mathescape, name=tidemark, basewidth = {.3em}]
test
\end{lstlisting}
\end{tcolorbox}
\end{xlrbox}
\begin{frame}[t]
\frametitle{Box recycling}
\xusebox{boxname}
\end{frame}
\end{document}
```

Answer #2
samcarter
Totally different approach:
save the content of your listing into a separate file, say `code.anush` and then use it like this:
```
\documentclass{beamer}
\beamertemplatenavigationsymbolsempty
\setbeamertemplate{frametitle}[default][center]
\usepackage[many]{tcolorbox}
\tcbuselibrary{listings}
\usepackage{listings}
\usepackage{xsavebox}
\lstdefinestyle{duckstyle}{%
moredelim=[is][\color{red}]{|}{|},
mathescape=true,
escapechar=@,
basicstyle=\ttfamily,
columns=fullflexible
}
\lstset{style=duckstyle}
\newtcbinputlisting{\mylisting}[2][]{%
listing options={mathescape, name=tidemark, basewidth = {.3em}},
listing only,
listing file={#2},
top=0pt,
left=5pt,
right=5pt,
colback=blue!5!white,
text height=4.3cm,
width=\linewidth,
#1
}
\begin{document}
\begin{frame}[fragile, t]{}
\begin{columns}[onlytextwidth]
\begin{column}{0.5\textwidth}
\mylisting{code.anush}
\end{column}
\pause
\begin{column}{0.5\textwidth}
\setbeamertemplate{itemize item}[circle]
\addtolength{\leftmargini}{-0.2cm}
\begin{itemize}[<+->]
\setlength\itemsep{0.7cm}
\item Some text
\end{itemize}
\end{column}
\end{columns}
\end{frame}
\end{document}
```

The advantage is that the box where it actually belongs, so no problems with the line width etc.
Answer #3
samcarter
A workaround with the `scontents` package:
```
\documentclass{beamer}
\beamertemplatenavigationsymbolsempty
\setbeamertemplate{frametitle}[default][center]
\usepackage[many]{tcolorbox}
\usepackage{listings}
\lstdefinestyle{duckstyle}{%
moredelim=[is][\color{red}]{|}{|},
mathescape=true,
escapechar=@,
basicstyle=\ttfamily,
columns=fullflexible
}
\lstset{style=duckstyle}
\usepackage{scontents}
\Scontents*{%
\begin{tcolorbox}[top=0pt, left=5pt,right=5pt, colback=blue!5!white, text height=5.5cm]
\begin{lstlisting}[ mathescape, name=tidemark, basewidth = {.3em}]
stream $\langle a_1, \dots, a_m \rangle$,$a_i \in [n]$
@\Proc{Initialise}@
Choose random $h:[n] \rightarrow [n]$
@\Proc{Simple-Count}@
Set $M = h(a_1)$
For each $i \geq 2$
if $h(a_i) < M$
set $M = h(a_i)$
return $\hat{d} = n/M$
\end{lstlisting}
\end{tcolorbox}
}
\newcommand{\Proc}[1]{\textsc{#1}}
\begin{document}
\begin{frame}[fragile, t]{First randomised algorithm}
\getstored[1]{contents}
\end{frame}
\end{document}
```
Answer #4
Skillmon
> What am I doing wrong?
Verbatim contents don't work when the argument got tokenized once by TeX (meaning passed in as a parameter to some macro) as the category codes get fixed during that process. The LaTeX macros `\sbox` and `\savebox` do grab the contents of the box once to inject some colour-safety code before and after them. At that moment the argument gets tokenized and `listings` can't do its thing anymore.
So what could you do? Either use some LaTeX construct which would allow the verbatim material to be typeset into a box without fixed category codes (like `lrbox`, `xlrbox` of the `xsavebox` package [as pointed out by marmot](https://topanswers.xyz/tex?q=1310#a1550), the `scontents` package [as pointed out by samcarter](https://topanswers.xyz/tex?q=1310#a1549)), or you could use the TeX construct directly instead.
Since the LaTeX ways are already shown, the following shows how one would do this with TeX syntax:
```
\documentclass{beamer}
\beamertemplatenavigationsymbolsempty
\setbeamertemplate{frametitle}[default][center]
\usepackage[many]{tcolorbox}
\usepackage{listings}
\lstdefinestyle{duckstyle}{%
moredelim=[is][\color{red}]{|}{|},
mathescape=true,
escapechar=@,
basicstyle=\ttfamily,
columns=fullflexible
}
\lstset{style=duckstyle}
\newsavebox{\boxname}
\newcommand{\Proc}[1]{\textsc{#1}}
\begin{document}
\setbox\boxname\hbox{%
\begin{tcolorbox}[top=0pt, left=5pt,right=5pt, colback=blue!5!white, text height=5.5cm]
\begin{lstlisting}[ mathescape, name=tidemark, basewidth = {.3em}]
test
\end{lstlisting}
\end{tcolorbox}%
}
\begin{frame}[fragile, t]{}
\usebox{\boxname}
\end{frame}
\end{document}
```