3

When there is some verbatim code inside a beamer frame, we need to use fragile option. But this fragile option often makes inverse search jump to wrong file.

Therefore I tried to replace fragile option with \scantokens command in the following example:

\documentclass{beamer}
\usepackage{verbatim}

\newcommand{\mycmd}[1]{%
\scantokens{\begin{verbatim}
#1
\end{verbatim}}}

\begin{document}

\begin{frame}
Before
\mycmd{
  \hello \world
}
After
\end{frame}

\end{document}

The above code indeed works. Now I want to define an environment rather than a command, but the following code causes an error.

! File ended while scanning text of \scantokens.

\documentclass{beamer}
\usepackage{verbatim}

\newenvironment{myenv}
{%
   \scantokens\bgroup\verbatim
}
{%
   \endverbatim\egroup
}

\begin{document}

\begin{frame}
Before
\begin{myenv}
  \hello \world
\end{myenv}
End
\end{frame}

\end{document}

What is the correct way to define such environment?

  • do you use luatex (makes the answer a bit easier if you do) – David Carlisle May 8 '17 at 8:38
  • @DavidCarlisle I use xelatex. – Z.H. May 8 '17 at 8:39
  • 1
    No, the “command version” doesn't work: it won't preserve line endings, for instance. Do you know about the semiverbatim environment of beamer? Maybe it can help. – egreg May 8 '17 at 8:54
3

This can't work for a couple of reasons.

First reason.

The syntax for \scantokens is

\scantokens <general text>

and a <general text> can start with an implicit brace, but cannot end with it: the termination must be an explicit brace.

Second reason

Line endings are managed before \scantokens does its job, so they're lost. Also your code with the command form will suffer of this problem. For instance

\documentclass{beamer}
\usepackage{verbatim}

\newcommand{\mycmd}[1]{%
\scantokens{\begin{verbatim}
#1
\end{verbatim}}}

\begin{document}

\begin{frame}
Before
\mycmd{
  \hello \world
  \hello \world
  \hello \world
}
After
\end{frame}

\end{document}

produces

enter image description here

Conclusion

You're doomed, sorry. There's no escape from the fragile option.

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.