边注中的 tcolorbox

边注中的 tcolorbox

我正在尝试创建一个命令,使我能够在文档的边缘添加一些“重要注释”。只要我使用框的“左”选项并且页码是奇数,我就能让它工作。但是当我尝试将其放在偶数页上时,无论我使用“左”还是“右”选项,它都不会显示任何内容。

\documentclass[a4paper,11pt]{book}

\usepackage[outer=5cm, heightrounded, marginparwidth=4cm, marginparsep=0.5cm]{geometry}


\usepackage[T1]{fontenc}        % to get Type 1 fonts
\usepackage[utf8]{inputenc}     % to enable non ASCII characters
\usepackage{lmodern}
\usepackage[english]{babel}
\usepackage{marginnote} 
\usepackage{xcolor}
\usepackage[most,breakable,many]{tcolorbox}
\tcbset{before skip=0pt,after skip=0pt} 
\usepackage{lipsum}         % dummy text

%%%%%%%%%%%%%%%%%%%%%%%%% Important Note %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\newcounter{important}                                                                                                                              
\newtcolorbox[use counter=important]                                                                                                                
  {important}[1][]                                                                                                                                          
  {title=Important Note~\thetcbcounter,                                                                                                         
   width=4cm,                                                                                                                                               
   left=0pt,                                                                                                                                                    
   right=0pt,                                                                                                                                               
   fonttitle=\bfseries\color{white},                                                                                                                    
   colframe=olive,                                                                                                                                      
   colback=olive!10,                                                                                                                                        
   #1                                                                                                                                                           
}                                                                                                                                                               
\newcommand\Limportant[3][]{%                                                                                                                   
  \marginnote[#1]{%                                                                                                                                 
    \makebox[0pt][l]{\begin{important}[label=#3]                                                                                                
    #2                                                                                                                                                          
    \end{important}}}}% 

\newcommand\Rimportant[3][]{%                                                                                                                   
  \marginnote[#1]{%                                                                                                                                 
    \makebox[0pt][r]{\begin{important}[label=#3]                                                                                                
    #2                                                                                                                                                          
    \end{important}}}}% 

\begin{document}
\lipsum[1]
\Limportant[-2cm]{\lipsum[75]}.
\vfill
\pagebreak

\lipsum[1]
\Rimportant{\lipsum[75]}.    

\end{document}

理想情况下,我想结合这两个选项,以便它自动决定应该放在哪一侧,但我还没有弄清楚如何计算 '$'\pagenumber modulo 2 '$',所以我可以将其作为带有案例的命令包含在内。

在同一个命令中我可能写错了什么,因为添加用于垂直调整的可选命令没有效果('$'\Limportant[-2cm]{\lipsum[75]}'$' 处的 -2cm)

答案1

无需使用if odd page等选项tcolorbox

参数等存在一些问题,例如错误使用\marginnote

\documentclass[a4paper,11pt]{book}

\usepackage[outer=5cm, heightrounded, marginparwidth=4cm, marginparsep=0.5cm]{geometry}


\usepackage[T1]{fontenc}        % to get Type 1 fonts
\usepackage[utf8]{inputenc}     % to enable non ASCII characters
\usepackage{lmodern}
\usepackage[english]{babel}
\usepackage{marginnote} 
\usepackage{xcolor}
\usepackage[most,breakable,many]{tcolorbox}
\tcbset{before skip=0pt,after skip=0pt} 
\usepackage{lipsum}         % dummy text

%%%%%%%%%%%%%%%%%%%%%%%%% Important Note %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\newcounter{important}                                                                                                                              
\newtcolorbox[use counter=important]                                                                                                                
{important}[1][]                                                                                                                                          
{title=Important Note~\thetcbcounter,                                                                                                         
  width=4cm,                                                                                                                                               
  left=0pt,                                                                                                                                                    
  right=0pt,                                                                                                                                               
  fonttitle=\bfseries\color{white},                                                                                                                    
  colframe=olive,                                                                                                                                      
  colback=olive!10,                                                                                                                                        
  #1                                                                                                                                                           
}                                                                                                                                                               

\newcommand{\isimportant}[3][]{%
  \ifodd\value{page}
  \marginnote{%
    \makebox[0pt][l]{%
      \begin{important}[label=#3]
        #2%
      \end{important}%
    }%
  }[\ifblank{#1}{0pt}{#1}]%
  \else
  \marginnote[%
  {%
    \makebox[0pt][r]{%
      \begin{important}[label=#3]%
        #2%
      \end{important}
    }%
  }]{}[\ifblank{#1}{0pt}{#1}]%
  \fi
}


\begin{document}
\lipsum[1]
\isimportant[-5cm]{\lipsum[75]}{Foo}
\vfill
\clearpage
\lipsum[1]
\isimportant[-2cm]{Something different}{Otherstuff}.    

\end{document}

在此处输入图片描述

相关内容