Beamerで\verbが使いたい
LaTeXでプレゼンテーション用スライドが楽に作れるBeamerですが、\verbを使用したときに以下のエラーが発生しました。
! Missing } inserted.
<inserted text>
}
l.44 \end{frame}
I've inserted something that you may have forgotten.
(See the <inserted text> above.)
With luck, this will get me unwedged. But if you
really didn't forget anything, try typing `2' now; then
my insertion and my current dilemma will both disappear.
! Extra }, or forgotten \endgroup.
\verb@egroup ...erb@balance@group \@empty \egroup
l.44 \end{frame}
I've deleted a group-closing symbol because it seems to be
spurious, as in `$x}$'. But perhaps the } is legitimate and
you forgot something else, as in `\hbox{$x}'. In such cases
the way to recover is to insert both the forgotten and the
deleted material, e.g., by typing `I$}'.
! LaTeX Error: \verb illegal in argument.
See the LaTeX manual or LaTeX Companion for explanation.
Type H <return> for immediate help.
...
l.44 \end{frame}
Try typing <return> to proceed.
If that doesn't work, type X <return> to quit.
調べたらlistingsでもこのように問題に陥るようです。
Beamerでこれらの環境は使えないのかと落胆していたのですが、frame環境のオプションにfragile
を指定すれば動作することが分かりました1。以下に使用例を示しておきます。
使用例
\begin{frame}[fragile]{verb, verbatim test}% fragileオプションが必要
\verb|aiueoあいうえお|
\begin{verbatim}
#include<iostream>
int main() {
cout << "Hello, World!" << endl;
return 0;
}
\end{verbatim}
\end{frame}
\begin{frame}[fragile]{listings test}% fragileオプションが必要
\begin{columns}
\begin{column}{.5\linewidth}
\begin{lstlisting}[language=C++,
basicstyle=\ttfamily\scriptsize,
commentstyle=\textit,
classoffset=1,
keywordstyle=\bfseries,
frame=tRBl,
framesep=5pt,
showstringspaces=false,
numbers=left,
stepnumber=1,
numberstyle=\tiny,
tabsize=2,
breaklines = true,
]
#include <iostream>
int main() {
cout << "Hello, World!" << endl;
return 0;
}
\end{lstlisting}
\end{column}
\begin{column}{.4\linewidth}
\begin{tcblisting}{colback=yellow!5,colframe=yellow!50!black,listing only,
title=tcolorbox \& minted, fonttitle=\bfseries,
listing engine=minted,minted language=C}
#include <stdio.h>
int main() {
int a = 10;
scanf("%d", &a);
printf("%d\n", a);
}
\end{tcblisting}
\end{column}
\end{columns}
\end{frame}
この使用例をOverleafプロジェクトにしたリンクを貼っておきます。
fragileオプションとは
verb(verbatim)やlistingsのようなコマンド・環境は、テキスト内容をそのまま出力します。これらの環境たちが含まれるフレームをタイプセットするためには、frameの内部処理を変える必要があります。その内部処理を切り替える方法がfragile
オプションです。
fragileオプションを指定したframeは、frameの終了を、
\end{⟨frame environment name⟩} のみを含む行が最初に出現した場所
とします。通常 ⟨environment name⟩ はframeであることから、以下の制約が課されることを意味します。
\end{frame} のみの行をフレーム内に入れることができない
どうしてもframe内で\end{frame}
と書きたい場合、2つの方法があります。
一つは、fragile=singleslide
と書く方法です。これは複数スライドからなるフレームを作成できないという制約があるものの、\end{frame}
をフレーム内に記述することができます。
もう一つは、⟨frame environment name⟩ を変えることです。
\newenvironment{slide}[1]
{\begin{frame}[fragile,environment=slide]
\frametitle{#1}}
{\end{frame}}
\begin{slide}{My title}
テキスト。
\end{slide}
のように新しいフレーム環境を作ることで、制約に引っかからず\end{frame}
が記述できるようになります。
まとめ
ぜひ、皆さんもverbやlistingsをBeamerに乗せて楽しいLaTeX生活を送ってみてください!
参考文献
Beamer – A LaTeX class for producing presentations and slides
Beamer の frame の fragile – fragile オプションのややこしい話 – (マクロツイーター)
-
パーセントエンコードされたURLを\urlコマンドや\hrefコマンドなどを用いて記載する場合にも、fragileオプションが必要になります ↩