4

I'm trying to get listings using tcolorbox for beamer documentclass. My MWE is given below but could not get it working. Any help will be highly appreciated. Thanks


MWE


\documentclass{article}
\usepackage{listings}
\usepackage{tcolorbox}
\tcbuselibrary{listings}%
\usepackage{xcolor}

\lstset{ % General setup for the package
    language={[LaTeX]TeX},
    basicstyle=\small\sffamily,
    numbers=left,
    numberstyle=\tiny,
    frame=tb,
    tabsize=4,
    columns=fixed,
    showstringspaces=false,
    showtabs=false,
    keepspaces,
    commentstyle=\color{red},
    keywordstyle=\color{blue}
}%



\tcbset{listing engine={listings}}


\usepackage[english]{babel}
\usepackage{blindtext}

\begin{document}

\section{First Section}

\blindtext

% Redefine the document environment within a group
\begingroup
\renewenvironment{document}{}{}
\renewcommand\documentclass[2][]{}

\begin{tcblisting}{colback=red!5!white,colframe=red!25,left=6mm,
listing options={style=tcblatex,numbers=left,numberstyle=\tiny\color{red!75!black}}}
\documentclass{beamer}
\begin{document}
    \begin{frame}
        \frametitle{First Frame}
            \blindtext  
    \end{frame}

    \begin{frame}
        \frametitle{Second Frame}
            \blindtext  
    \end{frame}

\end{document}

\end{tcblisting}

\end{document}
9
  • @DavidCarlisle: Listings for beamer documentclass. See between \begin{tcblisting} ... \end{tcblisting}.
    – MYaseen208
    Aug 18, 2014 at 10:23
  • related: tex.stackexchange.com/questions/6006/…
    – jub0bs
    Aug 18, 2014 at 10:32
  • @DavidCarlisle: The tcblisting environment does show the code listing as well it's output, but this can not work, when there is an additional \documentclass inside of the listing (as it is tried to setup/typeset this 'fake' document). For sure, if the documentclass is relaxed (well, expanding to nothing in this case), all beamer specific commands are unknown
    – user31729
    Aug 18, 2014 at 10:32
  • @ChristianHupfer ah thanks, I'll delete comment (maybe I'll read the listings doc one day:-) Aug 18, 2014 at 10:36
  • @DavidCarlisle: I think, this has nothing to do with listings package itself ... it's rather this special feature of the tcblisting environment from tcolorbox package, which causes this problem
    – user31729
    Aug 18, 2014 at 10:38

1 Answer 1

4

I would put the beamer example into a separate file which can be compiled for its own. Then, the source code and the beamer slides can be included into the target document.

Here, I used example01.tex for the beamer file:

% example01.tex
\documentclass{beamer}
\usepackage{blindtext}
\begin{document}
    \begin{frame}
        \frametitle{First Frame}
            \blindtext
    \end{frame}

    \begin{frame}
        \frametitle{Second Frame}
            \blindtext
    \end{frame}
\end{document}

After compilation of this file, include the the result with \tcbinputlisting. The two slides are loaded within the comment option:

\documentclass{article}
\usepackage{listings}
\usepackage{tcolorbox}
\tcbuselibrary{listings}%
\usepackage{xcolor}

\lstset{ % General setup for the package
    language={[LaTeX]TeX},
    basicstyle=\small\sffamily,
    numbers=left,
    numberstyle=\tiny,
    frame=tb,
    tabsize=4,
    columns=fixed,
    showstringspaces=false,
    showtabs=false,
    keepspaces,
    commentstyle=\color{red},
    keywordstyle=\color{blue}
}%



\tcbset{listing engine={listings}}


\usepackage[english]{babel}
\usepackage{blindtext}

\begin{document}

\section{First Section}

\blindtext

\tcbinputlisting{listing file=example01.tex,
listing and comment,
comment={\includegraphics[width=\the\dimexpr 0.5\linewidth-1mm\relax,page=1]{example01}\hfill%
  \includegraphics[width=\the\dimexpr 0.5\linewidth-1mm\relax,page=2]{example01}},
colback=red!5!white,colframe=red!25,left=6mm,
leftlower=0pt,rightlower=0pt,
listing options={style=tcblatex,numbers=left,numberstyle=\tiny\color{red!75!black}}}

\end{document}

This gives:

enter image description here

2
  • (+1) Thanks Prof. Dr. Thomas F. Sturm for your nice answer and help. Especially for such a wonderful package.
    – MYaseen208
    Aug 18, 2014 at 16:15
  • Please also guide me read external file in \texexptitled environment.
    – MYaseen208
    Aug 18, 2014 at 16:43

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .