4

I'm trying to create a new \lstlisting environment with an input that represents the caption. I'm trying to do the following, but I get a "suspected you have forgotten a }" error. Any advice?

\lstnewenvironment{textfile}[1]
{
\lstset{frame=shadowbox, caption=#1}
}
{
}

Here's how I am trying to use it:

\documentclass[11pt, letterpaper]{article}
\usepackage{listings} % Used for code blocks

%% Format the captions
\usepackage{caption} % Caption package
\DeclareCaptionFont{white}{ \color{white} }
\DeclareCaptionFormat{listing}{
  \colorbox[cmyk]{0.43, 0.35, 0.35,0.01 }{
    \parbox{\textwidth}{\hspace{15pt}#1#2#3}
  }
}
\captionsetup[lstlisting]{ font={tt}, labelformat=empty}

\begin{document}
\begin{textfile}[results.txt]
Album of the year: 24K Magic - Bruno Mars
Song of the year: Despacito - Justin Bieber
Best Rap Song: HUMBLE. - Kendrick Lamar
\end{textfile}
\end{document}
1
  • Make a complete example. That makes it much easier to test your issue. Apr 20, 2018 at 15:00

1 Answer 1

5

As it is defined (but missing in the 'MWE') the textfile listings environment requires a mandatory argument, but in the document it is called with an optional [...] one -- this is a syntax error.

Correcting to use {} removes the error.

\documentclass[11pt, letterpaper]{article}
\usepackage{listings} % Used for code blocks

%% Format the captions
\usepackage{caption} % Caption package
\DeclareCaptionFont{white}{ \color{white} }
\DeclareCaptionFormat{listing}{
  \colorbox[cmyk]{0.43, 0.35, 0.35,0.01 }{
    \parbox{\textwidth}{\hspace{15pt}#1#2#3}
  }
}
\captionsetup[lstlisting]{ font={tt}, labelformat=empty}

\lstnewenvironment{textfile}[1]
{
\lstset{frame=shadowbox, caption=#1}
}
{
}

\begin{document}
\begin{textfile}{results.txt}
Album of the year: 24K Magic - Bruno Mars
Song of the year: Despacito - Justin Bieber
Best Rap Song: HUMBLE. - Kendrick Lamar
\end{textfile}
\end{document}
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.