如果目录中的条目仅包含默认图像,则自动为其着色

如果目录中的条目仅包含默认图像,则自动为其着色

介绍

我正在处理一个包含许多图像的文档。如果这些图像位于我告诉 LaTeX 查找的文件夹中,它们就会插入到 .pdf 中。
如果没有这样的图像,它将被默认图像替换。现在,这些图像似乎位于不同的部分。

目标

有时有些部分(和子部分等)仅包含默认图像。我想自动用某种灰色标记目录中这些完全空白的部分(子部分等)的名称。标题本身不应更改。

方法与问题

\documentclass[a4paper, hidelinks]{scrartcl}

\usepackage{graphicx, mwe, hyperref, xcolor}
\usepackage{tocloft} %% ----- could be helpful here?

\newcommand\img[2][]{
\IfFileExists{#2}{
    \includegraphics[trim=0 0 0 -5,#1]{#2}
    }
{
    \includegraphics[trim=0 0 0  -5, width=.4\textwidth]{example-image} %% the default image in this case
}
}

\newcommand{\defineavailablecolor}[2]{
\colorlet{available#1}{#2}
\expandafter\def\csname isavailable#1\endcsname{
    \renewcommand{\cftsecfont}{\bfseries\color{available#1}}
    \renewcommand{\cftsubsecfont}{\color{available#1}}
    \renewcommand{\cftsubsubsecfont}{\color{available#1}}
    \renewcommand{\cftparafont}{\color{available#1}}}
}
\makeatletter
\newcommand{\isavailable}[1]{\addtocontents{toc}{\protect\@nameuse  {isavailable#1}}}%
\makeatother

\defineavailablecolor{yes}{black}
\defineavailablecolor{no}{gray}


\begin{document} 
\tableofcontents
\newpage

\section{First Section} %% ----------- Textcolor in toc should stay black
\subsection{First Subsection} %% ----- Textcolor in toc should stay black
\img[width=.4\textwidth]{example-image-a}
\img[width=.4\textwidth]{example-image-b}

\section{Second Section} %% ----------- Textcolor in toc should stay black
\subsection{Second Subsection} %% ----- Textcolor in toc should stay black
\img[width=.4\textwidth]{example-image-c}
\isavailable{no}
\subsubsection{First Subsubsection}
\img[width=.4\textwidth]{example-image-d}

\isavailable{no}
\section{Third Section} %% ---------- Textcolor in toc should becoma grey
\isavailable{no}
\subsection{Third Subsection} %% ---- Textcolor in toc should become grey
\img[width=.4\textwidth]{example-image-e}
\img[width=.4\textwidth]{example-image-f}

\end{document}

我曾尝试做类似的事情:在 Latex 中使用两种颜色自定义目录。不过,我仍然需要手动标记条目……

关于最后一点:也许可以包含一个 if 语句?它可以测试该部分中的所有图像(等等)是否可用。如果不是这种情况,请为目录条目着色。但我也不知道如何测试多幅图像……

有人知道如何解决这个问题吗?非常感谢!

答案1

这还不是完整的解决方案——由于某种原因,如果下一个\section没有等\subsection,则 ToC 颜色是错误的。

该解决方案用于xcntperchap跟踪每个部分、子部分和子子部分的图像数量。如果此数字为零,则更改 ToC 颜色。

\documentclass[a4paper, hidelinks]{article}

\usepackage{xcolor}
\usepackage{graphicx}
\usepackage{tocloft} %% ----- could be helpful here?
\usepackage{xcntperchap}%

\usepackage{xpatch}%

\newcounter{totalsection}%
\newcounter{totalsubsection}%
\newcounter{totalsubsubsection}%

\newcounter{imagecntr}[section]

\DeclareAssociatedCounters{section}{totalsection}
\DeclareAssociatedCounters{subsection}{totalsubsection}
\DeclareAssociatedCounters{subsubsection}{totalsubsubsection}

\RegisterCounters{section}{imagecntr}%
\RegisterCounters{subsection}{imagecntr}%
\RegisterCounters{subsubsection}{imagecntr}%

\DeclareRobustCommand{\changetocpagenumberfonts}{%
  \addtocontents{toc}{\protect\renewcommand\protect\cftsecpagefont{\bfseries\color{\toccolor}}%
    \protect\renewcommand\protect\cftsubsecpagefont{\bfseries\color{\toccolor}}%
    \protect\renewcommand\protect\cftsubsubsecpagefont{\bfseries\color{\toccolor}}%
    \protect\renewcommand\protect\cftparapagefont{\bfseries\color{\toccolor}}}%
}

\makeatletter
\def\@sect#1#2#3#4#5#6[#7]#8{%
  \gdef\toccolor{availableyes}%
  \ifnum #2>\c@secnumdepth
    \let\@svsec\@empty
    \else
    \refstepcounter{#1}%
    \ifnum\ObtainTrackedValueExp[\number\value{total#1}]{#1}{imagecntr} > 0\relax
    \gdef\toccolor{availableno}%
    \fi%
    \changetocpagenumberfonts%
    \protected@edef\@svsec{\@seccntformat{#1}\relax}%
    \fi
    \@tempskipa #5\relax
    \ifdim \@tempskipa>\z@
    \begingroup
      #6{%
        \@hangfrom{\hskip #3\relax\@svsec}%
        \interlinepenalty \@M #8\@@par}%
      \endgroup
      \csname #1mark\endcsname{#7}%
      \addcontentsline{toc}{#1}{%
        \texorpdfstring{%
          \ifnum #2>\c@secnumdepth \else
          \protect\color{\toccolor}%
          \protect\numberline{\csname the#1\endcsname}%
          \fi
          #7}{#7}%
      }%
    \else
    \def\@svsechd{%
      #6{\hskip #3\relax
        \@svsec #8}%
      \csname #1mark\endcsname{#7}%
      \addcontentsline{toc}{#1}{%
          \ifnum #2>\c@secnumdepth \else
          \texorpdfstring{%
          \protect\color{\toccolor}%
          \protect\numberline{\csname the#1\endcsname}{#7}}{#7}%
          \fi
        }}%
    \fi
    \@xsect{#5}}
  \makeatother


\usepackage{hyperref}


\makeatletter
\newcommand\img[2][]{%
  \IfFileExists{#2}{%
    \includegraphics[trim=0 0 0 -5,#1]{#2}%
  }{%
    \stepcounter{imagecntr}%
    \includegraphics[trim=0 0 0  -5, width=.4\textwidth]{example-image} %% the default image in this case
  }%
}
\makeatother

\AtBeginDocument{%
  \gdef\toccolor{availableyes}%
}


\newcommand{\defineavailablecolor}[2]{%
  \colorlet{available#1}{#2}%
}

\defineavailablecolor{yes}{black}
\defineavailablecolor{no}{gray}


\begin{document} 
\tableofcontents
\clearpage
\section{First Section} %% ----------- Textcolor in toc should stay black
\subsection{First Subsection} %% ----- Textcolor in toc should stay black
\img[width=.4\textwidth]{example-image-a}
\img[width=.4\textwidth]{example-image-b}

\section{Second Section} %% ----------- Textcolor in toc should stay black
\subsection{Second Subsection} %% ----- Textcolor in toc should stay black
\img[width=.4\textwidth]{example-image-c}
\subsubsection{First Subsubsection}
\img[width=.4\textwidth]{example-image-d}

\section{Third Section} %% ---------- Textcolor in toc should becoma grey
\subsection{Third Subsection} %% ---- Textcolor in toc should become grey
\img[width=.4\textwidth]{example-image-e}
\img[width=.4\textwidth]{example-image-f}

\section{Fourth section}
\img[width=.4\textwidth]{ente.jpg}
\subsection{Another subsection}
\img[scale=0.3]{ente.jpg}

\subsubsection{Another subsubsection}
\img[scale=0.3]{ente.jpg}

\subsubsection{Another subsubsection}
\img[scale=0.3]{ente.jpg}

\section{And again some section with a default image}
\img[width=.4\textwidth]{example-image-e}


\end{document}

在此处输入图片描述

相关内容