General\index swallows text in tcolorbox

LaTeX specific issues not fitting into one of the other forums of this category.
Post Reply
ckleiner
Posts: 7
Joined: Mon Jan 31, 2022 8:38 pm

\index swallows text in tcolorbox

Post by ckleiner »

Dear community

I am trying to create a command TESTCMD which creates a tcolorbox containing text provided as argument.

Additionally, the command uses \index to include the argument in the index at the end of the document.

Interestingly, there is no text shown in the colorbox, but the text is listed in the index.
If the instruction \index{#1} is replaced by {#1} (that is: \index is omitted}, it works as expected (but of course, the index it not built then).

I compile the code with the sequence pdflatex, then makeindex and then pdflatex again.

Find the code below; any help appreciated.

-- Christoph

Code: Select all

\documentclass{book}
\usepackage[many]{tcolorbox}
\usepackage{imakeidx}

\makeindex

\newcommand{\TESTCMD}[1]{
    \tcbset{
        enhanced,colframe=white,coltitle=black,%sharp corners,
        frame style={top color=red!30,bottom color=white},
        fonttitle=\bfseries,interior hidden,title hidden,
    }
    \begin{tcolorbox}
        {\bf \underline{\index{#1}}}
    \end{tcolorbox}
}

\begin{document}
    \TESTCMD{The text is swallowed}
    \printindex
\end{document}

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

User avatar
Stefan Kottwitz
Site Admin
Posts: 10320
Joined: Mon Mar 10, 2008 9:44 pm

\index swallows text in tcolorbox

Post by Stefan Kottwitz »

Hi Christoph,

the \index command just generates the index entry, not text in the box. You can write it like this:

Code: Select all

\begin{tcolorbox}
  {\bfseries\underline{#1\index{#1}}}
\end{tcolorbox}
Stefan
LaTeX.org admin
ckleiner
Posts: 7
Joined: Mon Jan 31, 2022 8:38 pm

\index swallows text in tcolorbox

Post by ckleiner »

Thank you Stefan - works like a charm.
I thought \index would also show the term being indexed in the text.

-- Christoph
User avatar
Stefan Kottwitz
Site Admin
Posts: 10320
Joined: Mon Mar 10, 2008 9:44 pm

\index swallows text in tcolorbox

Post by Stefan Kottwitz »

Hi Christoph,

you could create your own command that does both, such as:

Code: Select all

\newcommand*{\idx}[1]{\index{#1}{#1}}
and then you just write \idx{keyword} whenever you have a word to be indexed. This also allows to modify the design for all keywords if you like, such as

Code: Select all

\newcommand*{\idx}[1]{\index{#1}{\textbf{#1}}}
I like macros for such things because I can decide later to adjust the design for all keywords (thickness, color, or italic). Some examples are here: Stefan
LaTeX.org admin
Post Reply