0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Beamerで\verbとかlistingsとか使いたいとき

Last updated at Posted at 2025-05-02

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 オプションのややこしい話 – (マクロツイーター)

  1. パーセントエンコードされたURLを\urlコマンドや\hrefコマンドなどを用いて記載する場合にも、fragileオプションが必要になります

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?