更改自定义目录中的图像

更改自定义目录中的图像

这有效:

根据答案https://tex.stackexchange.com/a/14964/36296,我组装了一个 MWE,它可能看起来很丑,但工作正常。在文档的任何地方,我都可以用我的命令更改要插入到下一节的图片\setsectocdeco

\documentclass{book}

\usepackage{graphicx}
\usepackage{etoc}
\usepackage{mwe}

\etocsetstyle{chapter}
{}
{}
{%
    \sectocdeco%
    \etocname%
    \par%
}
{}

\DeclareRobustCommand{\sectocdeco}{}

\newcommand*{\setsectocdeco}[1]{%
    \addtocontents{toc}{\protect\renewcommand{\sectocdeco}{\protect #1}}%
}   

\setsectocdeco{\includegraphics[height=1cm]{example-image-a}}

\begin{document}

    \tableofcontents

    \chapter{Chapter 1}

    \setsectocdeco{\includegraphics[height=1cm]{example-image-b}}
    \chapter{Chapter 2}

\end{document}

在此处输入图片描述


这不会:

但是,如果我把这些命令放入我的真实 ToC(基于https://tex.stackexchange.com/a/163707/36296),图像将不会更新:

\documentclass{book}

\usepackage{xcolor}
\usepackage{framed}
\usepackage{etoc}
\usepackage{mwe}
\usepackage[linktoc=all, colorlinks=true, linkcolor=black]{hyperref}
\colorlet{toccolour}{blue}

\newlength{\tocmargin}

\newenvironment{tocleftbar}
    {\def\FrameCommand {%
            \hspace{\dimexpr\tocmargin+40pt\relax}%
            {\color{toccolour}\vrule width 2pt depth 6pt}\hspace{6pt}%
            \hspace{-\tocmargin}%
        }%
        \MakeFramed {\leftskip\tocmargin\rightskip\tocmargin\FrameRestore}%
    }
    {\endMakeFramed}

\newif\ifintocleftbar

\etocsetstyle{chapter}
   {\nobreak\etocskipfirstprefix}% don't break immediately after Part
   {\pagebreak[2]}% but allow break after section or subsection
   {\global\let\ETOCthepage\etocthepage%
    \global\let\ETOCthename\etocthename%
    \global\let\ETOCthenumber\etocthenumber%
    \global\let\ETOCthelink\etocthelink%
                \ifintocleftbar\end{tocleftbar}\fi%
                \kern\baselineskip%
                \begin{tocleftbar}
                    \intocleftbartrue
                    \makebox[0pt][r]{%
                        \null\hfill%
                        \ETOCthelink{%
                            \raisebox{-1.5cm}{%
                                \sectocdeco%
                                \hspace*{18pt}%
                            }}}%
                            \parbox[t]{\dimexpr\hsize-2\tocmargin\relax}{%
                                \LARGE\par%
                                \ETOCthelink{\ETOCthename\hspace{0.5em}\dotfill\hspace{0.5em} \nobreak{\etocpage}}%
                                \rule[-\baselineskip]{0pt}{0pt}%
                            }%
                            \par\normalsize\leftskip\dimexpr\leftskip+3em\relax
                        }
                        {\ifintocleftbar\end{tocleftbar}\fi
    }

\DeclareRobustCommand{\sectocdeco}{}

\newcommand*{\setsectocdeco}[1]{%
    \addtocontents{toc}{\protect\renewcommand{\sectocdeco}{\protect#1}}%
}   

\setsectocdeco{\includegraphics[height=1cm]{example-image-a}}

\begin{document}

    \tableofcontents

    \chapter{Chapter 1}

    \setsectocdeco{\includegraphics[height=1cm]{example-image-b}}
    \chapter{Chapter 2}

\end{document}

在此处输入图片描述


长话短说:为什么第二个例子中的图像没有改变?


用例:

解决方案不需要基于上述代码。我正在寻找可以转换的东西

\documentclass{book}

\usepackage{graphicx}
\usepackage{titlesec}

\titleformat{\chapter}[display]%
{}%
{\secdeco}%
{1em}%
{}%
[]  

\titleformat{name=\chapter,numberless}[display]
{}%
{\secdeco}%
{1em}%
{}%
[]

\newcommand{\secdeco}{\includegraphics[height=1cm]{example-image-a}}

\begin{document}

    \tableofcontents

    \addtocounter{page}{6}

    \chapter{Name of First Chapter}
    \section{Another test section}
    \section{Another test section}
    \subsection{A test subsection}
    \section{Another test section}
    \subsection{A test subsection}
    \subsection{A test subsection with a long title spanning more than one line in the table of contents}

    \renewcommand{\secdeco}{\includegraphics[height=1cm, width=0.5cm]{example-image-b}}
    \chapter{Name of Second Chapter}
    \section{A test section with a long title spanning more than one line in the table of contents}
    \subsection{A test subsection}
    \subsection{Another test subsection}
    \section{A test section}
    \section{Another test section}
    \subsection{A test subsection}
    \section{Another test section}
    \subsection{A test subsection}

    \renewcommand{\secdeco}{\includegraphics[height=1cm]{example-image-a}}
    \chapter{Chapter without Sections}

    \chapter*{Unnumbered Chapter}
    \addcontentsline{toc}{chapter}{Unnumbered Chapter}

\end{document}

放入目录,它看起来应该是这样的:

在此处输入图片描述

主要目标是:

  • 对于每一章,都应该有一条与该章的目录一样长的规则。
  • 如果图像的高度大于章节目录的空间,则规则应与图像一样长(仅适用于没有部分的章节)
  • 所有图片的高度都相同,但宽度可能不同。如果能右对齐就好了。
  • 该解决方案应该兼容\usepackage{titlesec}

(我很抱歉,这听起来很像“为我做这件事”,但我认为这会为可能的解决方案提供更多机会,而不是试图修复上面的代码)

答案1

干得好。

\documentclass[a4paper]{book}

\usepackage{xcolor}
\usepackage{geometry}
\usepackage{framed}
\usepackage{etoc}\etocglobaldefs
\usepackage{mwe}
\usepackage[linktoc=all, colorlinks=true, linkcolor=black]{hyperref}
\colorlet{tocleftbarcolour}{blue}

\newlength{\tocmargin}
\setlength{\tocmargin}{1cm}
%\parindent0pt

\newlength{\tocleftbarsep}
\setlength{\tocleftbarsep}{2pt}

\newlength{\tocleftbarwidth}
\setlength{\tocleftbarwidth}{2pt}
\newlength{\tocleftbardepth}
\setlength{\tocleftbardepth}{6pt}

\newenvironment{tocleftbar}
    {\def\FrameCommand {%
            \hspace{\dimexpr\tocmargin\relax}%
            \textcolor{tocleftbarcolour}
                  {\vrule width \tocleftbarwidth depth \tocleftbardepth\relax}%
            \hspace{\tocleftbarsep}%
            \hspace{-\dimexpr\tocmargin\relax}%
        }%
        \MakeFramed {\leftskip\tocmargin\rightskip\tocmargin
                     \FrameRestore}%
    }
    {\endMakeFramed}

\newif\ifintocleftbar
\newbox\tempdecobox

\newcommand*{\EmergencyEndLeftBarEnv}{%
    % previous chapter had no section
      \LARGE
      \vskip-\baselineskip
      \vskip-\lineskip % assume height of images > baseline
      \vskip-\parskip
      \vskip-\tocleftbardepth % for fun
      \noindent
      \vphantom{\usebox{\tempdecobox}}%
            % we close a group here hence \etocname, etc.. get lost
            % immediately thereafter; this is preemptively counteracted
            % by the \etocglobaldefs issued after loading etoc
    \end{tocleftbar}%
}

\etocsetstyle{chapter}
   {\nobreak\etocskipfirstprefix}% don't break immediately after Part
   {\pagebreak[2]}% but allow break after section or subsection
   {\ifintocleftbar\EmergencyEndLeftBarEnv\fi
    \begin{tocleftbar}
     \LARGE
     \intocleftbartrue
         \sbox\tempdecobox{\sectocdeco}%
         \edef\heightoflastbox{\the\ht\tempdecobox}%
     \noindent\makebox[0pt][r]
       {\smash
         {\raisebox{\dimexpr-\height+\ht\strutbox\relax}{\usebox{\tempdecobox}}}%
        \hspace{\dimexpr2\tocleftbarsep+\tocleftbarwidth\relax}}%
     \parbox[t]{\dimexpr\hsize-2\tocmargin\relax}
        {\strut\etocname
         \hspace{0.5em}\dotfill\nobreak\hspace{0.5em}\strut\etocpage}%
      \par
      \normalsize
      \leftskip\dimexpr\leftskip+\tocmargin\relax
   }
   {\ifintocleftbar\EmergencyEndLeftBarEnv\fi}

\etocsetstyle {section}
   {\nopagebreak[3]\vspace{1pt}\etocskipfirstprefix}
   {\vspace {1pt plus .2pt minus .2pt}}
   {\noindent\llap{\makebox[\tocmargin][l]{\etocnumber}}%
    \etocname
    \nobreak\hspace{0.5em}{\itshape\etocpage}\par }
   {\end{tocleftbar}}

\newcommand*{\setsectocdeco}[1]% need to make a global def here
% because the \end{tocleftbar} will close a group
  {\addtocontents{toc}{\gdef\string\sectocdeco{\protect#1}}}   

\begin{document}

\tableofcontents

\setsectocdeco{\includegraphics[height=2cm]{example-image-a}}
\chapter{This is the first chapter. No sections here.}

\setsectocdeco{\includegraphics[height=1cm]{example-image-b}}
\chapter{Another chapter}
\section{foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo
  foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo
  foo foo 
}
bar
\section{foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo
  foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo
  foo foo 
}
bar
\section{foo}
bar
\section{foo}
bar
\section{foo}
bar

\setsectocdeco{\includegraphics[height=2cm, width=1cm]{example-image-c}}
\chapter{The third chapter}
\section{foo}
bar
\section{foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo
  foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo
  foo foo 
}
bar
\section{foo}
bar

\setsectocdeco{\includegraphics[height=1.5cm]{example-image-16x10}}
\chapter{And this one is the last chapter}

\end{document}

在此处输入图片描述

答案2

这不是一个解决方案,而是朝着这个方向迈出的一步……太长了,无法发表评论。必须更改两件事才能恢复正确的图像。

1) 在\etocsetstyle宏中, 的两个实例都\end{tocleftbar}必须更改为 ,\endtocleftbar并且 的一个实例\begin{tocleftbar}必须更改为\tocleftbar

2)我必须关闭MakeFramed重新定义的环境

\def\MakeFramed#1{#1}
\def\endMakeFramed{}

这些更改导致恢复了正确的图形装饰。此时的结论是\MakeFramed ... \endMakeFramed环境是当前问题所在。目前我对该软件包的经验还不够,因此无法提出补救措施。

\documentclass{book}

\usepackage{xcolor}
\usepackage{framed}
\usepackage{etoc}
\usepackage{mwe}
\usepackage[linktoc=all, colorlinks=true, linkcolor=black]{hyperref}
\colorlet{toccolour}{blue}

\newlength{\tocmargin}

\newenvironment{tocleftbar}
    {\def\FrameCommand {%
            \hspace{\dimexpr\tocmargin+40pt\relax}%
            {\color{toccolour}\vrule width 2pt depth 6pt}\hspace{6pt}%
            \hspace{-\tocmargin}%
        }%
        \MakeFramed {\leftskip\tocmargin\rightskip\tocmargin\FrameRestore}%
    }
    {\endMakeFramed}
\def\MakeFramed#1{#1}
\def\endMakeFramed{}

\newif\ifintocleftbar

\etocsetstyle{chapter}
   {\nobreak\etocskipfirstprefix}% don't break immediately after Part
   {\pagebreak[2]}% but allow break after section or subsection
   {\global\let\ETOCthepage\etocthepage%
    \global\let\ETOCthename\etocthename%
    \global\let\ETOCthenumber\etocthenumber%
    \global\let\ETOCthelink\etocthelink%
                \ifintocleftbar\endtocleftbar\fi%
                \kern\baselineskip%
                \tocleftbar
                    \intocleftbartrue
                    \makebox[0pt][r]{%
                        \null\hfill%
                        \ETOCthelink{%
                            \raisebox{-1.5cm}{%
                                \sectocdeco%
                                \hspace*{18pt}%
                            }}}%
                            \parbox[t]{\dimexpr\hsize-2\tocmargin\relax}{%
                                \LARGE\par%
                                \ETOCthelink{\ETOCthename\hspace{0.5em}\dotfill\hspace{0.5em} \nobreak{\etocpage}}%
                                \rule[-\baselineskip]{0pt}{0pt}%
                            }%
                            \par\normalsize\leftskip\dimexpr\leftskip+3em\relax
                        }
                        {\ifintocleftbar\endtocleftbar\fi
    }

\DeclareRobustCommand{\sectocdeco}{}

\newcommand*{\setsectocdeco}[1]{%
    \addtocontents{toc}{\protect\renewcommand{\sectocdeco}{\protect#1}}%
}   

\setsectocdeco{\includegraphics[height=1cm]{example-image-a}}

\begin{document}

    \tableofcontents

    \chapter{Chapter 1}

    \setsectocdeco{\includegraphics[height=1cm]{example-image-b}}
    \chapter{Chapter 2}

    \setsectocdeco{\includegraphics[height=1cm]{example-image-c}}
    \chapter{Chapter 3}

\end{document}

在此处输入图片描述

相关内容