2

I want to defne a macro environment that uses the listings and tcolorbox packages, something akin to this:

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

\newenvironment{codeblock}[1]
  {\begin{tcblisting}{title=#1}}
  {\end{tcblisting}}

\begin{document}
\begin{codeblock}
code
\end{codeblock}
\end{document}

However, I get the following error message:

! File ended while scanning use of \next.

What do? I've tried searching around, but it seems to be a very general error message that has something to do with empty spaces in verbatim/listing environments.

I have tried implementing the code directly and it compiles. The error only arises when using the macro, and I'd much prefer using the macro considering I have a couple of hundred entries with the same formatting (not included for simplicity's sake).

1
  • 1
    Search for \newtcblisting in the tcolorbox documentation
    – mbert
    Nov 8, 2022 at 20:16

1 Answer 1

2

You need to define the new listing format/style via \newtcblisting:

enter image description here

\documentclass{article}

\usepackage{tcolorbox}
\tcbuselibrary{listings}

\newtcblisting{codeblock}[1]{title=#1}

\begin{document}

\begin{codeblock}{code title}
code
\end{codeblock}

\end{document}
2
  • 1
    I'd be tempted to use \newtcblisting{codeblock}[2][]{title=#2,#1} so that users could pass an optional argument to the environment containing per-block tweaks....?
    – cmhughes
    Nov 8, 2022 at 21:25
  • @cmhughes: Good point. It's always nice to add some flexibility.
    – Werner
    Nov 8, 2022 at 21:34

You must log in to answer this question.

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