6

I want to print a piece of verbatim within an item of an itemize environment, as below:

\documentclass[letter, 11pt]{article}
\usepackage{enumitem}
\usepackage{pifont}
\usepackage{listings}

\newcommand{\Question}[1]{{\bfseries?: #1\par\vspace{5pt}}}
\newcommand{\Answer}[1]{\noindent\ding{51}: #1}

\begin{document}

\section*{Problem \#1}
\Question{content
}

\Answer{
\begin{itemize}
    \item[1] Some content.\\
    My Verbatim:
    \begin{lstlisting}
        G = VVVVVVVVVVVVVVVVVVVVVVVVVVV

        PROJ = PPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP
    \end{lstlisting}
\end{itemize}
}
\end{document}

But there is an error:

! Paragraph ended before \lst@next was complete.

I did even try the frame solution, presented here, but it does still not work:

\documentclass[letter, 11pt]{article}
\usepackage{enumitem}
\usepackage{pifont}
\usepackage{listings}

\newcommand{\Question}[1]{{\bfseries?: #1\par\vspace{5pt}}}
\newcommand{\Answer}[1]{\noindent\ding{51}: #1}

\begin{document}

\section*{Problem \#1}
\Question{content
}

\Answer{
\begin{itemize}
    \item[1] Some content.\\
    My Verbatim:
    \begin{frame}[fragile]
        \begin{lstlisting}
            G = VVVVVVVVVVVVVVVVVVVVVVVVVVV

            PROJ = PPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP
        \end{lstlisting}    
    \end{frame}
\end{itemize}
}
\end{document}
5
  • Crosspost
    – Johannes_B
    Oct 18, 2016 at 19:30
  • @Johannes_B: I sent that question there, because no one declared any idea, here.
    – User
    Oct 18, 2016 at 19:32
  • Well, maybe that happened because you did not listen to helpers advice. You got two answers for the no line to end question, both of hem saying that an environment would be better suited for what you are trying to do. And here again, commands instead of environments.
    – Johannes_B
    Oct 18, 2016 at 19:38
  • @Johannes_B: I remember your advice within my question, considerably. The problem is that this code is a part of a big project and many people are working on that. For the sake of uniformity, we must use the commands, as they are defined, not to improve them. That is the reason behind not to applying your advice to use environment, instead of command. Actually, I need a way to handle the problem within this command form. Is it possible, at all?
    – User
    Oct 18, 2016 at 19:45
  • @Roboticist Did you ever resolve this? Aug 6, 2019 at 2:54

4 Answers 4

2

Since there seem to be an absolute No way for code improvement (including a change of macros) and manually doing stuff anyways, including numbering, there is nothing much that can be done.

\documentclass[letter, 11pt]{article}
\usepackage{enumitem}
\usepackage{pifont}
\usepackage{listings}

\newcommand{\Question}[1]{{\bfseries?: #1\par\vspace{5pt}}}
\newcommand{\Answer}{\noindent\ding{51}: }

\begin{document}

\section*{Problem \#1}
\Question{content
}

\Answer{
    \begin{itemize}
        \item[1] Some content.\\
            My Verbatim:
            \begin{lstlisting}
            G =
            VVVVVVVVVVVVVVVVVVVVVVVVVVV

            PROJ
            =
            PPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP
            \end{lstlisting}
    \end{itemize}
}
\end{document}
19

Not an answer to OP's question, but I got to this question when I forgot to add [fragile] to my frame when adding lstlisting code.

Example:

Broken

\begin{frame}
    \begin{lstlisting}[language=Java]
    // single line comment
    \end{lstlisting}
\end{frame}

Fixed

\begin{frame}[fragile]
    \begin{lstlisting}[language=Java]
    // single line comment
    \end{lstlisting}
\end{frame}
3

The workaround with \input also worked in my case.

I encountered the same error message when trying to use lstlistings within the multiaudience package:


\usepackage{multiaudience}
\SetNewAudience{guests}

\begin{shownto}{guests} 
  \begin{indentcode}
        \begin{lstlisting}
         some text
        \end{lstlisting}
  \end{indentcode}
\end{shownto}

~~~~~~~   example which works   ~~~~~~~~~

\usepackage{multiaudience}
\SetNewAudience{guests}

\begin{shownto}{guests} 
  \begin{indentcode}
        \input{folder/newfile}
  \end{indentcode}
\end{shownto}

with the content of newfile.tex being saved in the 'folder':
    \begin{lstlisting}
         some text
    \end{lstlisting}

2
  • 3
    could you please add a fully compilable answer to check the validity? Right now, it does not fully answer this question. May 3, 2019 at 12:43
  • Thanks. I have this issue with baposter class, moving the code out and using \input works!
    – Emit Taste
    Aug 24, 2020 at 15:02
1

What can you control, here?

The issue is with a verbatim environment as the argument to a command. From this question, one solution is to use cprotect:

\documentclass[letter, 11pt]{article}
\usepackage{enumitem,cprotect}
\usepackage{pifont}
\usepackage{listings}

\newcommand{\Question}[1]{{\bfseries?: #1\par\vspace{5pt}}}
\newcommand{\Answer}[1]{\noindent\ding{51}: #1}

\begin{document}

\section*{Problem \#1}
\Question{content
}

\cprotect\Answer{
\begin{itemize}
    \item[1] Some content.\\
    My Verbatim:
        \begin{lstlisting}
            G = VVVVVVVVVVVVVVVVVVVVVVVVVVV

            PROJ = PPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP
        \end{lstlisting}
\end{itemize}
}
\end{document}
0

You must log in to answer this question.

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