我希望在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}
\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}