奇数页和偶数页上可点击的章节

奇数页和偶数页上可点击的章节

遵循 Gonzalo Medina 提出的伟大思想每页右侧有可点击的章节,我想将其扩展到双面书。我做错了什么?似乎此代码并不关心页面是偶数还是奇数...

以下是代码:

      \documentclass[11pt,BCOR8mm,final,linktocpage,twoside,headsepline,a4paper]{scrbook}
     \usepackage{background}
     \usepackage{etoolbox}
     \usepackage{graphicx}
     \usepackage{totcount}
     \usepackage{lipsum}
     \usepackage{truncate}
     \usepackage{changepage}
     \usepackage{ifthen}
     \usepackage[hidelinks]{hyperref}
     \usepackage{scrextend}
     \usepackage{ifoddpage}
     \strictpagecheck


     \def\striptext to#1 #2{\def\striptextout{}\def\tmp{#1}\striptextA #2\relax\end}
     \def\striptextA{\futurelet\next\striptextB}
     \def\striptextB{\ifx\next\relax \expandafter \skiptoend
        \else \ifx\next\spacetoken
                  \edef\striptextoutA{\striptextout\space}%
                  \def\next{\afterassignment\striptextD \let\next= }%
              \else \def\next{\striptextC}%
        \fi   \fi
        \next
     }
     \def\striptextC#1{\edef\striptextoutA{\striptextout#1}\striptextD}
     \def\striptextD{\setbox0=\hbox{\striptextoutA}%
        \ifdim\wd0>\tmp\relax \expandafter \skiptoend
        \else \let\striptextout=\striptextoutA \expandafter \striptextA
        \fi
     }
     \def\skiptoend#1\end{}
     \edef\tmp{\let\noexpand\spacetoken= \space}\tmp




     % to have access to the total number of sections
     \regtotcounter{chapter}

     % every section starts on a new page
     \pretocmd{\chapter}{\clearpage}{}{}


     % auxiliary lengths for the height of the frame and the width of each tab
     \newlength\mylen
     \newlength\mylena

     % style for the section tabs
     \tikzset{
     tab/.style={
       text width=\mylena,
       draw=gray,
       thick,
       rectangle,
       rounded corners=2pt,  % 12pt
       align=center,
       text width=23pt,   
       inner sep=0pt,
       fill=gray!20,
       font=\sffamily\scriptsize
       }
     }
     % style for the current section tab
     \tikzset{selectedtab/.style={tab,color=white,fill=gray!90}}

     % the page number is showed in the background material
     \pagestyle{empty}

     % the main part; as background material we place the border, 
     % the section (current and other) tabs and the page number 
     \AtBeginDocument{
     % calculation of the width for each tab
     \setlength\mylen{\dimexpr\textheight+2cm\relax}
     \ifnum\totvalue{chapter}>0
     \setlength\mylena{\dimexpr\mylen/\totvalue{chapter}\relax}
     \fi
     \checkoddpage\ifoddpage%
     \backgroundsetup{
     scale=1,
     color=black,
     angle=0,
     opacity=1,
     contents= {
       \begin{tikzpicture}[remember picture, overlay]
       \node[
         inner sep=0pt,
         text width=\the\dimexpr\textwidth+1.5cm\relax
       ] 
         at (current page.center) (border) {\rule{0pt}{\dimexpr\textheight+2cm\relax}};
       \foreach \valchapter in {0,...,\numexpr\totvalue{chapter}-1\relax}
       {
         \node[
           \ifnum\thechapter<\numexpr\valchapter+1\relax 
             tab%
           \else 
           \ifnum\thechapter>\numexpr\valchapter+1\relax 
             tab%
           \else selectedtab%
           \fi\fi,
           minimum height=\mylena
           ] 
           at ([yshift=-(0.5+\valchapter)*\mylena]border.north east) 
           (tab-\valchapter) 
           {\hspace*{10pt}\rotatebox{+90}{%
            \hyperlink{sec:\valchapter}{{  \scriptext   {\nameref{sec:\the\numexpr\valchapter }}  \striptextout \ldots   }\relax}%
             }
           };
       }
       \node[
         draw=gray,
         line width=2pt,
         rectangle,
         rounded corners=10pt,
         inner sep=0pt,
         text width=\the\dimexpr\textwidth+1.5cm\relax,
         fill=white
       ] 
         at (current page.center) 
         {\rule{0pt}{\dimexpr\textheight+2cm\relax}};
       \node[font=\small\sffamily,fill=white]   % 
         at (border.south) 
         {\makebox[3em][c]{\thepage}};
     \end{tikzpicture}}
       }
     %}
     \else
     \egroup
     \backgroundsetup{
     scale=1,
     color=black,
     angle=0,
     opacity=1,
     contents= {
       \begin{tikzpicture}[remember picture, overlay]
       \node[
         inner sep=0pt,
         text width=\the\dimexpr\textwidth+1.5cm\relax
       ] 
         at ([xshift=-20.3cm] current page.center) (border) {\rule{0pt}{\dimexpr\textheight+2cm\relax}};
       \foreach \valchapter in {0,...,\numexpr\totvalue{chapter}-1\relax}
       {
         \node[
           \ifnum\thechapter<\numexpr\valchapter+1\relax 
             tab%
           \else 
           \ifnum\thechapter>\numexpr\valchapter+1\relax 
             tab%
           \else selectedtab%
           \fi\fi,
           minimum height=\mylena
           ] 
           at ([yshift=-(0.5+\valchapter)*\mylena]border.north east) 
           (tab-\valchapter) 
           {\hspace*{0pt}\rotatebox{-90}{%
            \hyperlink{sec:\valchapter}{{  \scriptext  {\nameref{sec:\the\numexpr\valchapter }}  \striptextout \ldots   }\relax}%  
             }
           };
       }
       \node[
         draw=gray,
         line width=2pt,
         rectangle,
         rounded corners=10pt,
         inner sep=0pt,
         text width=\the\dimexpr\textwidth+1.5cm\relax,
         fill=white
       ] 
         at (current page.center) 
         {\rule{0pt}{\dimexpr\textheight+2cm\relax}};
       \node[font=\small\sffamily,fill=white]   % 
         at (border.south) 
         {\makebox[3em][c]{\thepage}};
     \end{tikzpicture}}
       }
     \fi}
     %%%%%






     \begin{document}

     \chapter{Introduction}
     \label{sec:0}
     \hypertarget{sec:0}{}
     \lipsum[1-3]
     \chapter{Idea No 1}
     \label{sec:1}
     \hypertarget{sec:1}{}
     \lipsum[1-3]
     \chapter{Idea No 2}
     \label{sec:2}
     \hypertarget{sec:2}{}
     \lipsum[1-3]
     \chapter{Idea No 3}
     \label{sec:3}
     \hypertarget{sec:3}{}
     \lipsum[1-3]
     \chapter{Idea No 4}
     \label{sec:4}
     \hypertarget{sec:4}{}
     \lipsum[1-3]
     \chapter{TIdea No 5}
     \label{sec:5}
     \hypertarget{sec:5}{}
     \lipsum[1-3]

     \end{document}

答案1

背景包文档提供了一个示例(示例 4),说明当奇数页和偶数页需要不同的背景时如何进行;基本上,您需要在最后\AddEverypageHook使用钩子和。\BgMaterial

这是您的代码的简化版本(您的\scriptext\striptextout命令产生了错误,因此我抑制了它们):

\documentclass[11pt,BCOR8mm,final,linktocpage,twoside,headsepline,a4paper]{scrbook}
\usepackage[contents={}]{background}
\usepackage{etoolbox}
\usepackage{graphicx}
\usepackage{totcount}
\usepackage{lipsum}
\usepackage{truncate}
\usepackage{changepage}
\usepackage{ifthen}
\usepackage[hidelinks]{hyperref}
\usepackage{scrextend}
\usepackage{ifoddpage}
\strictpagecheck

\regtotcounter{chapter}

\pretocmd{\chapter}{\clearpage}{}{}

\newlength\mylen
\newlength\mylena

\tikzset{
tab/.style={
  text width=\mylena,
  draw=gray,
  thick,
  rectangle,
  rounded corners=2pt,  % 12pt
  align=center,
  text width=23pt,   
  inner sep=0pt,
  fill=gray!20,
  font=\sffamily\scriptsize
  }
}
\tikzset{selectedtab/.style={tab,color=white,fill=gray!90}}

\pagestyle{empty}

\AtBeginDocument{
  \setlength\mylen{\dimexpr\textheight+2cm\relax}
  \ifnum\totvalue{chapter}>0
    \setlength\mylena{\dimexpr\mylen/\totvalue{chapter}\relax}
  \fi
  \AddEverypageHook{
    \ifodd\value{page}%
      \backgroundsetup{
        scale=1,
        color=black,
        angle=0,
        opacity=1,
        contents={%
       \begin{tikzpicture}[remember picture, overlay]
       \node[
         inner sep=0pt,
         text width=\the\dimexpr\textwidth+1.5cm\relax
       ] 
         at (current page.center) (border) {\rule{0pt}{\dimexpr\textheight+2cm\relax}};
\foreach \valchapter in {0,...,\numexpr\totvalue{chapter}-1\relax}
       {
         \node[
           \ifnum\thechapter<\numexpr\valchapter+1\relax 
             tab%
           \else 
           \ifnum\thechapter>\numexpr\valchapter+1\relax 
             tab%
           \else selectedtab%
           \fi\fi,
           minimum height=\mylena
           ] 
           at ([yshift=-(0.5+\valchapter)*\mylena]border.north east) 
           (tab-\valchapter) 
           {\hspace*{10pt}\rotatebox{+90}{%
            \hyperlink{sec:\valchapter}{{ {\nameref{sec:\the\numexpr\valchapter }}   \ldots   }\relax}%
             }
           };
       }         
\node[
         draw=gray,
         line width=2pt,
         rectangle,
         rounded corners=10pt,
         inner sep=0pt,
         text width=\the\dimexpr\textwidth+1.5cm\relax,
         fill=white
       ] 
         at (current page.center) 
         {\rule{0pt}{\dimexpr\textheight+2cm\relax}};
       \node[font=\small\sffamily,fill=white]   % 
         at (border.south) 
         {\makebox[3em][c]{\thepage}};
         \end{tikzpicture}%        
        }
      }
    \else
      \backgroundsetup{
        scale=1,
        color=black,
        angle=0,
        opacity=1,
        contents= {%
       \begin{tikzpicture}[remember picture, overlay]
       \node[
         inner sep=0pt,
         text width=\the\dimexpr\textwidth+1.5cm\relax
       ] 
         at (current page.center) (border) {\rule{0pt}{\dimexpr\textheight+2cm\relax}};
\foreach \valchapter in {0,...,\numexpr\totvalue{chapter}-1\relax}
       {
         \node[
           \ifnum\thechapter<\numexpr\valchapter+1\relax 
             tab%
           \else 
           \ifnum\thechapter>\numexpr\valchapter+1\relax 
             tab%
           \else selectedtab%
           \fi\fi,
           minimum height=\mylena
           ] 
           at ([yshift=-(0.5+\valchapter)*\mylena]border.north west) 
           (tab-\valchapter) 
           {\hspace*{-10pt}\rotatebox{+90}{%
            \hyperlink{sec:\valchapter}{{ {\nameref{sec:\the\numexpr\valchapter }}   \ldots   }\relax}%
             }
           };
       }         
\node[
         draw=gray,
         line width=2pt,
         rectangle,
         rounded corners=10pt,
         inner sep=0pt,
         text width=\the\dimexpr\textwidth+1.5cm\relax,
         fill=white
       ] 
         at (current page.center) 
         {\rule{0pt}{\dimexpr\textheight+2cm\relax}};
       \node[font=\small\sffamily,fill=white]   % 
         at (border.south) 
         {\makebox[3em][c]{\thepage}};
         \end{tikzpicture}%        
        }
      }
    \fi\BgMaterial
  }
}

\begin{document}

\chapter{Introduction}
\label{sec:0}
\hypertarget{sec:0}{}
\lipsum[1-3]
\chapter{Idea No 1}
\label{sec:1}
\hypertarget{sec:1}{}
\lipsum[1-3]
\chapter{Idea No 2}
\label{sec:2}
\hypertarget{sec:2}{}
\lipsum[1-3]
\chapter{Idea No 3}
\label{sec:3}
\hypertarget{sec:3}{}
\lipsum[1-3]
\chapter{Idea No 4}
\label{sec:4}
\hypertarget{sec:4}{}
\lipsum[1-3]
\chapter{TIdea No 5}
\label{sec:5}
\hypertarget{sec:5}{}
\lipsum[1-3]

\end{document}

在此处输入图片描述

评论

在发货时页码是已知的,因此没有必要,\checkoddpage...并且简单\ifodd测试是完全安全的。

相关内容