CTeX-org / ctex-kit

Macro Packages and Scripts for Chinese TeX users
980 stars 124 forks source link

xpinyin automatically applied to header when spanning multiple pages #669

Open hungtzuchang opened 1 year ago

hungtzuchang commented 1 year ago

When pinyinscope environment spans multiple pages, pinyin is automatically applied to the header of the pages (see pictures below, compiled with MikTeX on Windows). This happens when using non-A4 paper sizes in documentclass setting or using a different font size. image MWE:

% !TEX TS-program = xelatex
\documentclass[12pt,b5paper]{ctexbook}
\usepackage{zhlipsum,xpinyin}
\begin{document}
\chapter{亂碼}
\begin{pinyinscope}
\zhlipsum
\end{pinyinscope}
\end{document}

image MWE:

% !TEX TS-program = xelatex
\documentclass[12pt,a4paper]{ctexbook}
\usepackage{zhlipsum,xpinyin}
\begin{document}
\chapter{亂碼}
\begin{pinyinscope}
\Large \zhlipsum
\end{pinyinscope}
\end{document}
muzimuzhi commented 1 year ago

Class options 12pt,b5paper make both page header and footer affected, while b4paper only makes page footer affected.

I guess the cause is similar to https://tex.stackexchange.com/q/498857, in which if the change of catcode table in main body spans multiple pages, then because the page header and footer are only typeset when a page is ship out, the current catcode table when they are typeset is inherited from the main body, hence also the modified one.

In current issue, what's inherited from main body is the state of \enablepinyin that pinyinscope environment turns on locally. Wrapping page header and footer in \disablepinyin ... \enablepinyin is a workaround (actually only \disablepinyin is enough because header and footer are all saved in a box first and boxes have an implicit level of grouping), for example using fancyhdr package.

Header Footer
before image image
after
(with fancyhdr)
image image
% !TEX TS-program = xelatex
\documentclass[12pt,b5paper]{ctexbook}
\usepackage{zhlipsum,xpinyin}
\usepackage{fancyhdr}

\fancypagestyle{plain}{%
  \fancyhf{}%
  \fancyfoot[C]{\disablepinyin\thepage}%
}

\pagestyle{fancy}

% not essentially the same as original "headings" page style
\fancyhf{}
\fancyhead[LE,RO]{\disablepinyin\thepage}
\fancyhead[RE]{\disablepinyin\slshape\leftmark}
\fancyhead[LO]{\disablepinyin\rightmark}
\renewcommand{\headrulewidth}{0pt}

\renewcommand{\thepage}{第 \arabic{page} 页}

\begin{document}
\chapter{亂碼}
\begin{pinyinscope}
\zhlipsum[1-3]
\end{pinyinscope}
\end{document}