10 \addtolength 这一命令是局部生效的,如何能使其在全局生效?

发布于 2023-05-03 17:17:38

我希望在titleformat 中的长度设置在titleformat 外也可以生效,该怎么做?代码如下。

\documentclass[b5paper,svgnames,10pt]{book}
\usepackage[utf8x]{inputenc}
\usepackage[title]{appendix}
\usepackage{imakeidx}
\makeindex
\usepackage{tikz,calc}
\usepackage[explicit]{titlesec}
\usepackage{xcolor}
\usepackage{colortbl}
\usepackage[margin=1.5cm]{geometry}
\usepackage{lipsum}
\usepackage{newtxtext,newtxmath}

%%%%%%%%%%%%%%%%%% fancy heading  \chapter %%%%%%%%%%%%%%%%%%%%%%%%
\newlength{\testlen}
\setlength{\testlen}{2cm}
\newcommand*\chapterlabel{}
\titleformat{\chapter}
{\gdef\chapterlabel{}
  \normalfont\sffamily\Huge\bfseries\scshape}
{\gdef\chapterlabel{\thechapter\ }}{0pt}
{\addtolength{\testlen}{1cm}
\begin{tikzpicture}[remember picture,overlay]
    \node[yshift=-3cm] at (current page.north west)
    {\begin{tikzpicture}[remember picture, overlay]
        \draw[fill=LightSkyBlue] (0,0) rectangle
        (\paperwidth,3cm);
        \node[anchor=east,xshift=.9\paperwidth,rectangle,
        rounded corners=20pt,inner sep=11pt,
        fill=MidnightBlue]
        {\color{white}\chapterlabel#1};
      \end{tikzpicture}
    };
  \end{tikzpicture}
}

\titlespacing*{\chapter}{0pt}{50pt}{0pt}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{document}

\tikz{
\draw[blue](0,0)--(\testlen,0);
\draw[blue](0,-2cm)--(3cm,-2cm);
}

\end{document}

查看更多

关注者
0
被浏览
873
1 个回答
雾月
雾月 2023-05-03
这家伙很懒,什么也没写!
\documentclass{article}
% \usepackage{calc} % calc 必须放在前面
\usepackage{regexpatch}
\makeatletter
\NewCommandCopy{\gaddtolength}{\addtolength}
\xpatchcmd{\gaddtolength}{\advance}{\global\advance}{}{\ERROR}
\makeatother

\newlength\testlen

\begin{document}

\setlength\testlen{0pt}
\begingroup
\addtolength\testlen{10pt} % 局部设置
\endgroup
\the\testlen

\setlength\testlen{0pt}
\begingroup
\gaddtolength\testlen{10pt} % 全局设置
\endgroup
\the\testlen

\end{document}

image.png

撰写答案

请登录后再发布答案,点击登录

发布
问题

分享
好友

手机
浏览

扫码手机浏览