samcarter
Using the `compilable listing` option from tcolorbox is an easy way to include examples using other document classes, e.g.
```
% !TeX TS-program = latexmk -time -pdf -pdflatex="pdflatex -synctex=1 -interaction=nonstopmode -shell-escape %%O %%S" % | txs:///view-log | txs:///view-pdf
\documentclass{article}
\usepackage[most]{tcolorbox}
\begin{document}
\begin{tcblisting}{
externalize listing=test,
listing and comment,
pdf comment,
freeze pdf,
compilable listing,
run pdflatex,
}
\documentclass{beamer}
\begin{document}
\begin{frame}
test
\end{frame}
\end{document}
\end{tcblisting}
\end{document}
```
The one thing that bugs me a bit is that this will result in tons of extra files in the working directory. Is there any way to outsource the generated files into a subfolder, e.g. similar to what one would get with `\tcbset{external/prefix=./build/}` for externalized tikz pictures?
Top Answer
samcarter
The following hack works on \*unix systems. Make sure the subfolder already exists before compiling.
- by changing `listing file` the .tex file will be generated in the sub folder. This alone wouldn't sufficient, as latex would still be run from the main directory and all the auxiliary files would be generated there
- in the second step, I'm prepending `run pdflatex` to first change into the sub folder. Alternatively one could look into setting the aux and out directories for the latex run, but just switching into the subfolder seemed easier
```
% !TeX TS-program = latexmk -time -pdf -pdflatex="pdflatex -synctex=1 -interaction=nonstopmode -shell-escape %%O %%S" % | txs:///view-log | txs:///view-pdf
\documentclass{article}
\usepackage[most]{tcolorbox}
\newcommand{\prefix}{./build}
\tcbset{
compilable listing/.style={listing file={\prefix/\jobname-listing-\thetcblistingcount.tex}},
run pdflatex/.style={run@system@command={cd\space \prefix;\space pdflatex}{\jobname-listing-\thetcblistingcount.tex}},%
}
\begin{document}
\begin{tcblisting}{
externalize listing=test,
listing and comment,
pdf comment,
freeze pdf,
compilable listing,
run pdflatex,
}
\documentclass{beamer}
\begin{document}
\begin{frame}
test
\end{frame}
\end{document}
\end{tcblisting}
\end{document}
```