使用 mdframed 格式化索引条目

使用 mdframed 格式化索引条目

我正在尝试使用mdframe来格式化索引条目。如果我\def\UseMdframeForFormatting{}在下面的 MWE 中取消注释 (它尝试使用mdframe),则会出现错误:

\@index 的使用与其定义不符

代码:

%\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

不是带有mdframed而是带有tcolorbox,这是可行的,如果\begin{tcolorbox}\protected。 的风格实际上 tcolorbox应该与 相适应。\newtcolorbox

\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}

更新

\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}

在此处输入图片描述

相关内容