3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

TeXエラー:「Too many }'s かつ \color@endbox ->\color@endgroup \egroup」の解決案

3
Last updated at Posted at 2020-02-01

はじめに

それまでは図を入れずに (\begin{figure}で始まる文なしに) TeXファイルのコンパイルができていたのに,\begin{figure}で図を挿入しようとしたら次のようなエラーが出てなかなか原因が分からなかったかつ,日本語でまとめている記事を見つけられなかったので,まとめておく.

...
! Too many }'s.
\color@endbox ->\color@endgroup \egroup

先に結論

を知りたければ,まとめのみ見れば良い.

問題例

環境はWindows10で,コンパイルはptex2pdfをVS Codeで次のようにレシピを作成して2回コンパイル.

settings.json
        {
            "name": "ptex2pdf",
            "command": "ptex2pdf",
            "args": [
                "-l",
                "-ot",
                "-kanji=utf8 -synctex=1",
                "%DOC%"
            ]
        },

これは処理系を (コンパイル方法を) e-pTeXにすることに該当して,当のe-pTeXは次の通り.コマンドはplatexである.ptex2pdfはここでは「pLaTeXというマクロ体系を使って,e-pTeXという処理系による処理=コンパイルを行った結果作られたdviファイルを,dvipdfmxというソフトウェアを使ってPDFファイルに変換する,という一連の流れをまとめたLuaスクリプト」として扱われる (はず...間違っていたら教えて下さい).

cmd
C:\Users\UserName>platex -v
e-pTeX 3.14159265-p3.8.0-180226-2.6 (utf8.sjis) (TeX Live 2018/W32TeX)
kpathsea version 6.3.0
ptexenc version 1.3.6
Copyright 2018 D.E. Knuth.

で,次のTeXコードのように,\begin{figure}が記述されたファイルを上記レシピでコンパイルすると,Too many ...エラーが生じる.\begin{figure}から\end{figure}を削除すると,コンパイルは通ってPDFが無事に作成される.当然,tigerはご降臨されない.

tiger.tex
\documentclass[twocolumn,a4j,9pt,oneside,final]{jarticle} %platex

\usepackage[dvipdfmx]{graphicx}% for hyperref
\usepackage[dvipdfmx, bookmarkstype=toc, colorlinks=false, pdfborder={0 0 0}, bookmarks=false, bookmarksnumbered=true]{hyperref}
\usepackage{pxjahyper}
\usepackage{color} %校正での目印等に利用 \textcolor{red}{hogehoge}

\usepackage{doublespace}

\begin{document}
    これはサンプルである.
    これはサンプルである.
    これはサンプルである.
    これはサンプルである.
    これはサンプルである.

    \begin{figure}[bt]
        \centering
        \includegraphics[keepaspectratio=true,width=0.47\textwidth]{tiger.eps}
        \caption{This is a tiger.}
    \end{figure}

\end{document}
...
! Too many }'s.
\color@endbox ->\color@endgroup \egroup

ちなみにtigerがご降臨されていないPDFファイルはこんな感じ.

no_tiger_doublespace_pdf

妥協解決案

[TeX のエラーメッセージ - TeX Wiki](https://texwiki.texjp.org/?TeX のエラーメッセージ)で「Too many }'s」を検索しても,「開き括弧 { と閉じ括弧 } の対応がとれていない場合に現れるエラーメッセージ(のひとつ)で, 単に閉じ括弧のほうが多い(というだけの情報しかない)場合に現れます.」とあるだけ.

そこで,StackExchange: Error message: too many }'s [closed]の中のanswerにある

Most probably it is the package xthesis that contains some pieces of code to redefine \@xfloat in a bad way. It seems that many university's thesis templates contain such a definition from a file named doublespace.sty. The solution is to (re)define \@xfloat in a "good" way, for example: a work around here

で,doublespace.styに言及しているし,試しにと思って\usepackage{doublespace}をコメントアウトしてみたらコンパイルに成功した.tigerがご降臨された.

tiger_singlespace_pdf

しかし,行間が狭くなってしまう (当然である).それでもよければ\usepackage{doublespace}を削除するのが一番シンプルな解決方法である.

まじめな解決案

先程のStackExchange: Error message: too many }'s [closed]の中のanswerにあるリンク

for example: a work around here

を見ると,サンプルらしきものがある.どうやら\@xfloatなるものを定義し直しているっぽいので,真似てみる.以下の%Save LaTeX kernel version of \@xfloat部分と,% Create a modified copy of \@xfloat using the kernel definition部分がそれである.

tiger.tex
%Save LaTeX kernel version of \@xfloat
\makeatletter
\let\my@xfloat\@xfloat
\makeatother

\documentclass[twocolumn,a4j,9pt,oneside,final]{jarticle} %platex

\usepackage[dvipdfmx]{graphicx}% for hyperref
\usepackage[dvipdfmx, bookmarkstype=toc, colorlinks=false, pdfborder={0 0 0}, bookmarks=false, bookmarksnumbered=true]{hyperref}
\usepackage{pxjahyper}
\usepackage{color} %校正での目印等に利用 \textcolor{red}{hogehoge}

\usepackage{doublespace}

% Create a modified copy of \@xfloat using the kernel definition
\makeatletter
\def\@xfloat#1[#2]{
	\my@xfloat#1[#2]%
	\def\baselinestretch{1}%
	\@normalsize \normalsize
}
\makeatother

\begin{document}
    これはサンプルである.
    これはサンプルである.
    これはサンプルである.
    これはサンプルである.
    これはサンプルである.

    \begin{figure}[bt]
        \centering
        \includegraphics[keepaspectratio=true,width=0.47\textwidth]{tiger.eps}
        \caption{This is a tiger.}
    \end{figure}

\end{document}

これで無事,行間は2行のまま,tigerがご降臨された.

tiger_doublespace_pdf

補足にもならないもの

悪いのはdoublespace.styで,この中で\@xfloatという変数 (...か?) を「bad way」で定義しているのが原因らしい.で,それを「good way」で定義し直したのが,まじめな解決案で示したコードである.

ということがStackExchange: Error message: too many }'s [closed]に書かれていたが,具体的にどう「bad」なのかは分からない (あまり分かる気もないが).ついでにいうと\@xfloatの再定義の中身も分かっていない.いなくても何とかなってしまった.

まとめ

TeXで\begin{figure}コードを入れた際,Too many }'s かつ \color@endbox ->\color@endgroup \egroupエラーが出た場合,2行間隔でなくともよいなら,\usepackage{doublespace}を削除すれば解決する.

より一般的な解決方法としては,LaTeX Community: Re: color package incompatible with figures?にあるように,

%Save LaTeX kernel version of \@xfloat
\makeatletter
\let\my@xfloat\@xfloat
\makeatother

\documentclassより上に書いて,

% Create a modified copy of \@xfloat using the kernel definition
\makeatletter
\def\@xfloat#1[#2]{
	\my@xfloat#1[#2]%
	\def\baselinestretch{1}%
	\@normalsize \normalsize
}
\makeatother

\usepackage{doublespace}より下,\begin{document}より上に書く.

3
1
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
3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?