tabularx 内の \index

tabularx 内の \index

by 陽炎 -
Number of replies: 4
tabularx 環境内で \index を使ったときの挙動について質問させてください.

以下のような例を処理したとき,
.idx ファイルに書き出される \indexentry の内容を見ると,
tabularx 環境内の \index では内容のマクロが展開されてしまい,
異なる索引項目となってしまいます.

マクロ \hoge の定義方法を \DeclareRobustCommand に変更したところ,
マクロの展開は防げましたが,余計な空白が入ってしまい,
やはり異なる索引項目となってしまいます.

テキスト処理で .idx ファイル中の余計な空白を取り除けば応急処置にはなりますが,
作業が増えてわずらわしいので,もっとうまい解決方法はないでしょうか.

\documentclass[a4paper, 10pt]{jsarticle}

\usepackage{tabularx}

\usepackage{makeidx}
\makeindex

\def\hoge#1{\sffamily #1}
%\DeclareRobustCommand{\hoge}[1]{\sffamily #1}

\begin{document}

\index{hoge@\hoge{hoge}}%
hoge

\begin{tabularx}{\textwidth}{llX}
\index{hoge@\hoge{hoge}}%
 hoge & hoge & hoge \\
\end{tabularx}

\printindex

\end{document}


上記により生成された .idx ファイルの内容
\indexentry{hoge@\hoge{hoge}}{1}
\indexentry{hoge@\sffamily hoge}{1}

\hoge\DeclareRobustCommand で定義したときの .idx ファイルの内容
\indexentry{hoge@\hoge{hoge}}{1}
\indexentry{hoge@\hoge {hoge}}{1}

なお,環境は Debian (lenny) で ptexlive (for TeX Live 2009) を使っています.

以上
In reply to 陽炎

Re: tabularx 内の \index

by 匿 名 -
質問の内容と意図がよくわからない(tabularx環境でindexの挙動が変化するのは理解できました)ので回答はパスしますが、気になる点をコメントします。

\def\hoge#1{\sffamily #1}
ではなく、
\def\hoge#1{{\sffamily #1}}
としないとhoge以降すべてがsffamilyになりませんか?

質問の
\indexentry{hoge@\sffamily hoge}{1}

\indexentry{hoge@{\sffamily hoge}}{1}
と展開されたら問題ないような気がしますが。。。

In reply to 匿 名

Re: tabularx 内の \index

by 陽炎 -
> \def\hoge#1{\sffamily #1}
> ではなく、
> \def\hoge#1{{\sffamily #1}}
> としないとhoge以降すべてがsffamilyになりませんか?

すみません.ケアレスミスですね.
投稿にしか使わないダミーマクロなので深く考えていませんでした.


質問の意図を改めて説明しますと,
\documentclass[a4paper, 10pt]{jsarticle}

\usepackage{tabularx}

\usepackage{makeidx}
\makeindex

\def\hoge#1{{\sffamily #1}}
% \DeclareRobustCommand{\hoge}[1]{{\sffamily #1}}

\begin{document}

\index{hoge@\hoge{hoge}}%
hoge

\clearpage %% 追加 %%

\begin{tabularx}{\textwidth}{llX}
\index{hoge@\hoge{hoge}}%
 hoge & hoge & hoge \\
\end{tabularx}

\printindex

\end{document}
のような例について,生成された .idx ファイルは
以下のようになってほしいのに,
\indexentry{hoge@\hoge{hoge}}{1}
\indexentry{hoge@\hoge{hoge}}{2}
以下のようになってしまう.
\indexentry{hoge@\hoge{hoge}}{1}
\indexentry{hoge@{\sffamily hoge}}{2}
すると,mendex の実行結果は,
以下のようになってほしいのに,
\begin{theindex}

 \item \hoge{hoge}, 1, 2

\end{theindex}
以下のようになってしまう,ということです.
\begin{theindex}

 \item \hoge{hoge}, 1
 \item {\sffamily hoge}, 2

\end{theindex}
In reply to 陽炎

Re: tabularx 内の \index

by 匿 名 -
検索しましたら,次のような回答がヒットしました(tabularx ではなく \caption の引数についてではありますが):

-------------------------------------------------------------------------
Newsgroups: comp.text.tex
From: Heiko Oberdiek <heiko.oberd...@googlemail.com>
Date: Wed, 21 Apr 2010 12:35:59 +0200
Subject: Re: problems with makeindex

-------------------------------------------------------------------------
Dr Engelbert Buxbaum <engelbert_buxbaum@xxxxxxxxxxx> wrote:

> - identical \index{xxx} commands give 2 separate entries in the index,
> if one of them occurs in text, the other in the caption of a float.

\index reads its argument as verbatim text that means it changes
the catcodes before. However inside a caption these catcode
changes do not have an effect, because the argument is
already read by \caption.

There are several options:
* Ensure that all \index commands read their argument in the
same way, e.g. move the \index out of the caption.
* Or use \string inside the index argument in caption:
\caption[...]{...\index{Hello \string\emph{World}}}

【…中略…】

--
Heiko Oberdiek

-------------------------------------------------------------------------
In reply to 匿 名

Re: tabularx 内の \index

by 陽炎 -
\string を挿入することで解決しました.
\index や,引用記事にもある \caption のほかに,
\multicolumn などでも同じ現象が起きており,同様に解決しました.
ありがとうございました.