4

I am trying to use an mdframe to format an index entry. If I uncomment the \def\UseMdframeForFormatting{} in the MWE below (which attempts to use an mdframe), then I get the error:

Use of \@index doesn't match its definition

Code:

%\def\UseMdframeForFormatting{}%
\documentclass{article}
\usepackage{mdframed}
\usepackage{imakeidx}
\usepackage{xparse}
\usepackage{hyperref}
\usepackage{bookmark}

\newcommand*\lettergroup[1]{\subsection{#1}}

\newcommand*{\IndexWithName}[2]{%
    % #1 = word to index
    % #2 = index name
    \index[#2]{#1}%
}%

\newcommand*{\FormatIndexEntry}[1]{%
    \ifdefined\UseMdframeForFormatting% <--- If this is defined, things no longer work
        \begin{mdframed}[userdefinedwidth=0.5\linewidth]
            \textcolor{red}{#1}%
        \end{mdframed}%
    \else
        \textcolor{red}{#1}%
    \fi
}%


\NewDocumentCommand{\AddIndexEntry}{%
    O{}% #1 = index name
      m% #2 = word to index this under
      m% #3 = indexed term
}{%
    \expandafter\IndexWithName\expandafter{%
        #2!\FormatIndexEntry{#3}%
    }{#1}%
}
\newcommand{\indexopt}[2]{\index[#2]{#1}}

\makeindex[title={Main Index},columns=1,program=texindy]
\makeindex[title={Name Index},columns=1,program=texindy,name=Name]
\indexsetup{level=\section}

\begin{document}

%% This works fine here, but not above to format index entry
\begin{mdframed}[userdefinedwidth=0.5\linewidth]
    \textcolor{red}{foo}%
\end{mdframed}%

\AddIndexEntry[Name]{zero}{zero sum}

\clearpage 
\setcounter{secnumdepth}{0}
%\printindex
\printindex[Name]
\end{document}
1
  • \protect\begin{mframed} etc.? But this will leave a missing item error – user31729 Feb 6 '16 at 11:17
4

Not with mdframed but with tcolorbox, this is working, if \begin{tcolorbox} is \protected. The style of the tcolorbox should be adapted with \newtcolorbox actually.

\def\UseMdframeForFormatting{}%
\documentclass{article}
\usepackage{mdframed}
\usepackage[most]{tcolorbox}

\usepackage{imakeidx}
\usepackage{xparse}
\usepackage{hyperref}
\usepackage{bookmark}

\newcommand*\lettergroup[1]{\subsection{#1}}

\newcommand*{\IndexWithName}[2]{%
  % #1 = word to index
  % #2 = index name
  \index[#2]{#1}%
}%

\makeatletter
\newcommand{\FormatIndexEntry}[1]{%
  \@ifundefined{UseMdframeForFormatting}{% <--- If this is defined, things no longer wor
    \textcolor{red}{#1}%
  }{%
    \protect\begin{tcolorbox}[colframe={gray},nobeforeafter,box align=center,sharp corners,width=0.5\linewidth]%
      \protect\textcolor{red}{#1}%
    \protect\end{tcolorbox}%
  }%
}%
\makeatother

\NewDocumentCommand{\AddIndexEntry}{%
    O{}% #1 = index name
      m% #2 = word to index this under
      m% #3 = indexed term
}{%
  \expandafter\IndexWithName\expandafter{%
    #2!\FormatIndexEntry{#3}%
  }{#1}%
}
\newcommand{\indexopt}[2]{\index[#2]{#1}}


\makeindex[title={Main Index},columns=1,program=texindy]
\makeindex[title={Name Index},columns=1,name=Name,program=texindy]
\indexsetup{level=\section}

\begin{document}

%% This works fine here, but not above to format index entry
\begin{mdframed}[userdefinedwidth=0.5\linewidth]\textcolor{red}{foo}\end{mdframed}%

\AddIndexEntry[Name]{zero}{zero sum}
%\index[Name]{zero}

\clearpage 
\setcounter{secnumdepth}{0}
%\printindex
\printindex[Name]
\end{document}

Update

\documentclass{article}
\usepackage[most]{tcolorbox}

\usepackage{imakeidx}
\usepackage{xparse}
\usepackage{hyperref}
\usepackage{bookmark}


\def\UseMdframeForFormatting{}%

\newcommand*\lettergroup[1]{\subsection{#1}}

\newcommand*{\IndexWithName}[2]{%
  % #1 = word to index
  % #2 = index name
  \index[#2]{#1}%
}%

\newtcolorbox{indexentrybox}[1][]{%
  nobeforeafter,
  boxrule=0.5pt,
  boxsep=0pt,
  width=0.5\linewidth,
  box align=center,
  sharp corners,
  colframe={gray},
  coltext={red},
  colback=white,
  #1,
}%

\makeatletter
\newcommand{\FormatIndexEntry}[1]{%
  \@ifundefined{UseMdframeForFormatting}{% <--- If this is defined, things no longer wor
    \textcolor{red}{#1}%
  }{%
    \protect\begin{indexentrybox}
      #1%
      \protect\end{indexentrybox}
  }%
}%
\makeatother

\NewDocumentCommand{\AddIndexEntry}{%
    O{}% #1 = index name
      m% #2 = word to index this under
      m% #3 = indexed term
    }{%
      \expandafter\IndexWithName\expandafter{%
        #2!\FormatIndexEntry{#3}}{#1}%
    }
\newcommand{\indexopt}[2]{\index[#2]{#1}}


\makeindex[title={Main Index},columns=1,program=texindy]
\makeindex[title={Name Index},columns=1,name=Name,program=texindy]
\indexsetup{level=\section}

\begin{document}
Foo

\AddIndexEntry[Name]{zero}{zero sum}
\clearpage 
\setcounter{secnumdepth}{0}
%\printindex
\printindex[Name]

\end{document}

enter image description here

6
  • Yep, this works. Although in my application I needed to include \usepackage{morewrites}. If an mdframed specific solution does not show up I'll switch to tcolorbox. – Peter Grill Feb 6 '16 at 12:29
  • @PeterGrill: I'll try to provide for mdframed as well, but I suspect, there's something going wrong after \endmdframe. I could not figure out yet – user31729 Feb 6 '16 at 12:32
  • I guess you ran into difficulties with mdframed version? – Peter Grill Feb 16 '16 at 11:43
  • @PeterGrill: I did not pursue -- sorry, I was busy with other things recently. Shall I go on? – user31729 Feb 16 '16 at 11:44
  • Nah, if you are busy with other things thats ok. I guess I could play around with tcolorbox. – Peter Grill Feb 16 '16 at 11:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.