1

In highlighting math, JPi defined \hlfancy a breakable box to highlight text involving math.

In How to refer to indexed words, Andrew defined \INDEX and \DEX to refer to indexed words which work fine when the reference is in a tcolorbox or in a \colorbox but not in a \hlfancy box where it causes an error.

To see, uncomment %\hlfancy{yellow}{A reference in an hlfancybox: \DEX[aaaa]{aaaC}} in the following minimum working example:

\documentclass[11pt]{book}

\usepackage{makeidx}
\usepackage{tcolorbox}
\usepackage{xcolor}
%Begin breakable hlfancy boxes. See https://tex.stackexchange.com/questions/318991/highlighting-math
\usepackage{soul}
\newcommand{\mathcolorbox}[2]{\colorbox{#1}{$\displaystyle #2$}}
\newcommand{\hlfancy}[2]{\sethlcolor{#1}\hl{#2}}
%End breakable hlfancy boxes.

    \usepackage{makeidx}
    \makeindex
    \usepackage{tcolorbox}
%==================Begin HYPERREF
    \usepackage[hyperfootnotes=false]{hyperref}%[pdfborder={0 0 0}]

%Begin the ``quick and dirty hack'' due to Heiko Oberdiek which loads showidx
\makeatletter
\ifHy@hyperindex
  \def\HyInd@ParenLeft{(}%
% Hook in \HyInd@@wrindex
  \let\HyInd@showidx\@empty
% Hook in \HyInd@@wrindex for package showidx
  \def\HyInd@showidx#1{%
    \@showidx{#1}%
    \if@nobreak
      \ifvmode
        \nobrak
      \fi
    \fi
  }%
% Load package showidx
  \let\siOrg@makeindex\makeindex
  \let\siOrg@@index\@index
  \let\siOrg@@wrindex\@wrindex
  \let\siOrg@index\index
  \RequirePackage{showidx}
  \let\makeindex\siOrg@makeindex
  \let\@index\siOrg@@index
  \let\@wrindex\siOrg@@wrindex
  \let\index\siOrg@index
% rest of hyperref part
  \@ifpackageloaded{multind}{%
    \let\HyInd@org@wrindex\@wrindex
    \def\@wrindex#1#2{\HyInd@@wrindex{#1}#2||\\}%
    \def\HyInd@@wrindex#1#2|#3|#4\\{%
      \ifx\\#3\\%
        \HyInd@org@wrindex{#1}{#2|hyperpage}%
      \else
        \def\Hy@temp@A{#3}%
        \ifx\Hy@temp@A\HyInd@ParenLeft
          HyInd@org@wrindex{#1}{#2|#3hyperpage}%
        \else
          \HyInd@org@wrindex{#1}{#2|#3}%
        \fi
      \fi
    }%
  }{%
    \def\@wrindex#1{\@@wrindex#1||\\}
    \def\@@wrindex#1|#2|#3\\{%
      \ifx\\#2\\%
        \protected@write\@indexfile{}{%
          \string\indexentry{#1|hyperpage}{\thepage}%
        }%
      \else
        \def\Hy@temp@A{#2}%
        \ifx\Hy@temp@A\HyInd@ParenLeft
          \protected@write\@indexfile{}{%
             \string\indexentry{#1|#2hyperpage}{\thepage}%
          }%
        \else
          \protected@write\@indexfile{}{%
            \string\indexentry{#1|#2}{\thepage}%
          }%
        \fi
      \fi
      \endgroup
      \HyInd@showidx{#1}%
      \@esphack
    }%
  }%
\fi
\makeatother
%End the ``quick and dirty hack'' due to Heiko Oberdiek which loads showidx

    \usepackage{cleveref}
%Begin the Sharpe code as modified by Schulz (2016-02-1)
\let\oldopenparen\(
\let\oldcloseparen\)
\def\({\protect\oldopenparen}
\def\){\protect\oldcloseparen}
%End the Sharpe code as modified by Schulz (2016-02-1)
%Begin referring code
\newcommand{\INDEX}[2][]{%
\if\relax\detokenize{#1}\relax\index{#2}\label{ind:#2}%
   \else\index{#2}\label{ind:#1}\fi%
}
\newcommand{\DEX}[2][]{%
   \if\relax\detokenize{#1}\relax\hyperref[ind:#2]{#2} (\Cpageref{ind:#2})%
   \else\hyperref[ind:#1]{#2} (\Cpageref{ind:#1})\fi%
}%
%End referring code
\newcounter{abc}%
\newtcolorbox%
[%begin code for tcolorbox
use counter= abc,
number within=chapter,
number freestyle={\noexpand\thechapter.\noexpand\arabic{\tcbcounter}~\noexpand\mytitleagree},%Used to make for LONG titles in the references.
crefname={definition}{definitions},
Crefname={Definition}{Definitions},
]%endForReferences
{greement}
[2][]%
{%
detach title,
coltitle=black,
nameref={#2},%
before upper={\tcbtitle\quad},
code={\gdef\mytitleagree{#2}},%Used to make for LONG titles in the references.
title=DEFINITION \thetcbcounter,%
#1,%Required for references.
}%
%End code for  tcolor box
\makeindex
\begin{document}

A word \textbf{aaaa}\INDEX[aaaa]{aaaa} in some text 

\begin{greement}[label={abc:3-1}]{Title}
A reference \DEX[aaaa]{aaaA} in a tcolorbox 
\end{greement}

\colorbox{yellow}{A reference in a colorbox: \DEX[aaaa]{aaaB}}

%\hlfancy{yellow}{A reference in an hlfancybox: \DEX[aaaa]{aaaC}}

\printindex
\end{document}
7
  • I am the other schremmer but I do not know how to rectify my error.
    – schremmer
    Commented Nov 10, 2017 at 0:19
  • 1
    @Cragfelt: It is the same user named schremmer https://tex.stackexchange.com/questions/393821/how-to-refer-to-indexed-words but logged in as other user... don't ask why
    – user31729
    Commented Nov 10, 2017 at 14:39
  • @ChristianHupfer Here is how if not why: I forgot to enter my password and StackExchange created a new account with the same name and same email address. No idea how to correct things. I wish I could.
    – schremmer
    Commented Nov 10, 2017 at 17:08
  • @schremmer: Please contact the SE staff in ask them about a merging of your accounts then
    – user31729
    Commented Nov 10, 2017 at 17:52
  • 1
    @Cragfelt As you can see, done deed. Thanks.
    – schremmer
    Commented Nov 12, 2017 at 0:06

1 Answer 1

1

As mentioned in the documentation of soul: Some things don't work. If you want to use such command inside a soul command you must hide them inside a \mbox. And yes this prevents line breaks for this command and so is not a solution for longer texts:

\documentclass[11pt]{book}

\usepackage{makeidx}
\usepackage{tcolorbox}
\usepackage{xcolor}
%Begin breakable hlfancy boxes. See https://tex.stackexchange.com/questions/318991/highlighting-math
\usepackage{soul}
\newcommand{\mathcolorbox}[2]{\colorbox{#1}{$\displaystyle #2$}}
\newcommand{\hlfancy}[2]{\sethlcolor{#1}\hl{#2}}
%End breakable hlfancy boxes.

    \usepackage{makeidx}
    \makeindex
    \usepackage{tcolorbox}
%==================Begin HYPERREF
    \usepackage[hyperfootnotes=false]{hyperref}%[pdfborder={0 0 0}]

%Begin the ``quick and dirty hack'' due to Heiko Oberdiek which loads showidx
\makeatletter
\ifHy@hyperindex
  \def\HyInd@ParenLeft{(}%
% Hook in \HyInd@@wrindex
  \let\HyInd@showidx\@empty
% Hook in \HyInd@@wrindex for package showidx
  \def\HyInd@showidx#1{%
    \@showidx{#1}%
    \if@nobreak
      \ifvmode
        \nobrak
      \fi
    \fi
  }%
% Load package showidx
  \let\siOrg@makeindex\makeindex
  \let\siOrg@@index\@index
  \let\siOrg@@wrindex\@wrindex
  \let\siOrg@index\index
  \RequirePackage{showidx}
  \let\makeindex\siOrg@makeindex
  \let\@index\siOrg@@index
  \let\@wrindex\siOrg@@wrindex
  \let\index\siOrg@index
% rest of hyperref part
  \@ifpackageloaded{multind}{%
    \let\HyInd@org@wrindex\@wrindex
    \def\@wrindex#1#2{\HyInd@@wrindex{#1}#2||\\}%
    \def\HyInd@@wrindex#1#2|#3|#4\\{%
      \ifx\\#3\\%
        \HyInd@org@wrindex{#1}{#2|hyperpage}%
      \else
        \def\Hy@temp@A{#3}%
        \ifx\Hy@temp@A\HyInd@ParenLeft
          HyInd@org@wrindex{#1}{#2|#3hyperpage}%
        \else
          \HyInd@org@wrindex{#1}{#2|#3}%
        \fi
      \fi
    }%
  }{%
    \def\@wrindex#1{\@@wrindex#1||\\}
    \def\@@wrindex#1|#2|#3\\{%
      \ifx\\#2\\%
        \protected@write\@indexfile{}{%
          \string\indexentry{#1|hyperpage}{\thepage}%
        }%
      \else
        \def\Hy@temp@A{#2}%
        \ifx\Hy@temp@A\HyInd@ParenLeft
          \protected@write\@indexfile{}{%
             \string\indexentry{#1|#2hyperpage}{\thepage}%
          }%
        \else
          \protected@write\@indexfile{}{%
            \string\indexentry{#1|#2}{\thepage}%
          }%
        \fi
      \fi
      \endgroup
      \HyInd@showidx{#1}%
      \@esphack
    }%
  }%
\fi
\makeatother
%End the ``quick and dirty hack'' due to Heiko Oberdiek which loads showidx

    \usepackage{cleveref}
%Begin the Sharpe code as modified by Schulz (2016-02-1)
\let\oldopenparen\(
\let\oldcloseparen\)
\def\({\protect\oldopenparen}
\def\){\protect\oldcloseparen}
%End the Sharpe code as modified by Schulz (2016-02-1)
%Begin referring code
\newcommand{\INDEX}[2][]{%
\if\relax\detokenize{#1}\relax\index{#2}\label{ind:#2}%
   \else\index{#2}\label{ind:#1}\fi%
}
\newcommand{\DEX}[2][]{%
   \if\relax\detokenize{#1}\relax\hyperref[ind:#2]{#2} (\Cpageref{ind:#2})%
   \else\hyperref[ind:#1]{#2} (\Cpageref{ind:#1})\fi%
}%
%End referring code
\newcounter{abc}%
\newtcolorbox%
[%begin code for tcolorbox
use counter= abc,
number within=chapter,
number freestyle={\noexpand\thechapter.\noexpand\arabic{\tcbcounter}~\noexpand\mytitleagree},%Used to make for LONG titles in the references.
crefname={definition}{definitions},
Crefname={Definition}{Definitions},
]%endForReferences
{greement}
[2][]%
{%
detach title,
coltitle=black,
nameref={#2},%
before upper={\tcbtitle\quad},
code={\gdef\mytitleagree{#2}},%Used to make for LONG titles in the references.
title=DEFINITION \thetcbcounter,%
#1,%Required for references.
}%
%End code for  tcolor box
\makeindex


\begin{document}

A word \textbf{aaaa}\INDEX[aaaa]{aaaa} in some text

\begin{greement}[label={abc:3-1}]{Title}
A reference \DEX[aaaa]{aaaA} in a tcolorbox
\end{greement}

\colorbox{yellow}{A reference in a colorbox: \DEX[aaaa]{aaaB}}

\hlfancy{yellow}{A reference in an hlfancybox: \mbox{\DEX[aaaa]{aaaC}}}

\printindex
\end{document}

enter image description here

3
  • Fisher Of course it doesn't break inside the mbox---so maybe you should change your wording above, but it sure does exactly what I needed. Thank you very much.
    – schremmer
    Commented Nov 25, 2017 at 17:59
  • A lot of people complain later that text doesn't break in a box, so I thought it would better to say it directly. Commented Nov 25, 2017 at 18:39
  • I meant that, since it doesn't not prevent the rest of the text in the hlfancy box from breaking, I would add "in the mbox" to " this prevents ... longer texts". But then, that's just me and, anyway, it worked fine for me. Thanks again.
    – schremmer
    Commented Nov 26, 2017 at 19:44

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .