Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

关于多行标题居中悬挂对齐在目录中的换行问题 #111

Closed
lingr7 opened this issue Jan 6, 2020 · 9 comments
Closed

关于多行标题居中悬挂对齐在目录中的换行问题 #111

lingr7 opened this issue Jan 6, 2020 · 9 comments
Labels
CJK/CTeX-collection Related to the CTeX macro collection. Layout/headings related to \part \chapter \section ...

Comments

@lingr7
Copy link

lingr7 commented Jan 6, 2020

为了实现多行标题的居中悬挂对齐:

\usepackage{varwidth} %% 提供 varwidth 环境
\ctexset{
chapter/name = {第,回},
chapter/titleformat = \chaptertitleformat
}
\newcommand\chaptertitleformat[1]{%% 以标题内容为参数
\begin{varwidth}[t]{.7\linewidth}#1\end{varwidth}}
......
\chapter{情中情因情感妹妹\\错里错以错劝哥哥}

以上代码来自cetx.pdf(Texlive2019携带的文档)
正文效果挺好,回目我用chapter/name = {\kaishu 第,回},
红楼梦第一回

\@writefile{toc}{\contentsline {chapter}{\numberline {第一回\hspace {.3em}}甄士隐梦幻识通灵\\贾雨村风尘怀闺秀}{3}{chapter.1}\protected@file@percent }
在chapter01.aux文件中的\\换行符造成在正式目录中,也出现了一个换行效果。

文档
目录换行了
红楼梦排版的github仓库地址
想问一下是否有办法解决目录中也换行了这个问题。

@stone-zeng
Copy link
Member

\documentclass{ctexbook}
% Fandol 缺字
% \setCJKmainfont{Source Han Serif TC}[ItalicFont=*]
\ctexset{
  chapter/name = {第, 回},
  chapter/titleformat = \chaptertitleformat
}
\newcommand\chaptertitleformat[1]{%
  \parbox[t]{8\ccwd}{#1}}

\begin{document}

\tableofcontents

\chapter{甄士隱夢幻識通靈\hspace{.5em}賈雨村風塵懷閨秀}
\chapter{賈夫人仙逝揚州城\hspace{.5em}冷子興演說榮國府}
\chapter{金陵城起複賈雨村\hspace{.5em}榮國府收養林黛玉}
\chapter{薄命女偏逢薄命郎\hspace{.5em}葫蘆僧亂判葫蘆案}
\chapter{開生面夢演紅樓夢\hspace{.5em}立新場情傳幻境情}
\chapter{賈寶玉初試雨雲情\hspace{.5em}劉姥姥一進榮國府}

\end{document}

目录:

image

章标题:

image

页眉:

image

手写 \hspace 比较暴力,但总之是能用的。觉得不好,也可以加点代码处理一下。

lingr7 referenced this issue in lingr7/HongLouMeng-ZhiPingBen Jan 6, 2020
@lingr7
Copy link
Author

lingr7 commented Jan 6, 2020

目录
目前是目录还是有点小问题,比如“目录”二字和章回标题对齐。

@Liam0205
Copy link
Contributor

Liam0205 commented Apr 23, 2020

\CTEXifname 即可解决。

\documentclass{ctexbook}
\usepackage{geometry}
\geometry{showframe}
\ctexset{
  chapter = {
    format += \centering,
    name = {第,回},
    nameformat += \chapternameformat,
    titleformat = \chaptertitleformat
  }
}
\newcommand{\chapternameformat}[1]{\kaishu #1}
\newcommand{\chaptertitleformat}[1]{%
  \parbox[t]{8\ccwd}{\CTEXifname{\relax}{\centering}#1}}

\begin{document}

\tableofcontents

\chapter{甄士隱夢幻識通靈\hspace{.5em}賈雨村風塵懷閨秀}
\chapter{賈夫人仙逝揚州城\hspace{.5em}冷子興演說榮國府}
\chapter{金陵城起複賈雨村\hspace{.5em}榮國府收養林黛玉}
\chapter{薄命女偏逢薄命郎\hspace{.5em}葫蘆僧亂判葫蘆案}
\chapter{開生面夢演紅樓夢\hspace{.5em}立新場情傳幻境情}
\chapter{賈寶玉初試雨雲情\hspace{.5em}劉姥姥一進榮國府}

\end{document}

image
image

@Liam0205 Liam0205 transferred this issue from CTeX-org/ctex-kit Apr 23, 2020
@Liam0205 Liam0205 added CJK/CTeX-collection Related to the CTeX macro collection. Layout/headings related to \part \chapter \section ... labels Apr 23, 2020
@Liam0205
Copy link
Contributor

另外,用 chapter/name = {\kaishu 第,回} 来解决 chapter name 的楷书不是个好做法。因为它把内容和格式混一起了。

@muzimuzhi
Copy link
Collaborator

@lingr7

目前是目录还是有点小问题,比如“目录”二字和章回标题对齐。

  • @Liam0205 的回复解决了「目录」二字的问题,让它们保持了居中。
  • 目录中的章回标题对齐效果是受 ctex/ctexheading 宏包影响后的效果(参考此文), 但 ctex 包未提供完整的目录条目样式定制功能。可以引入 tocloft 宏包处理
\usepackage[titles]{tocloft}
\setlength{\cftchapnumwidth}{5\ccwd}

完整例子

\documentclass{ctexbook}
\usepackage[showframe]{geometry}

\usepackage[titles]{tocloft}
\setlength{\cftchapnumwidth}{5\ccwd}

\ctexset{
  chapter = {
    format += \centering,
    name = {第,回},
    nameformat += \kaishu,
    titleformat = \chaptertitleformat
  }
}

\newcommand{\chaptertitleformat}[1]{%
  \CTEXifname{\parbox[t]{8\ccwd}{#1}}{#1}}

\begin{document}

\tableofcontents

\chapter{甄士隱夢幻識通靈\hspace{.5em}賈雨村風塵懷閨秀}
\chapter{賈夫人仙逝揚州城\hspace{.5em}冷子興演說榮國府}
\chapter{金陵城起複賈雨村\hspace{.5em}榮國府收養林黛玉}
\addtocounter{chapter}{10}
\chapter{薄命女偏逢薄命郎\hspace{.5em}葫蘆僧亂判葫蘆案}
\chapter{開生面夢演紅樓夢\hspace{.5em}立新場情傳幻境情}
\chapter{賈寶玉初試雨雲情\hspace{.5em}劉姥姥一進榮國府}

\end{document}

image

@muzimuzhi
Copy link
Collaborator

手写 \hspace 比较暴力,但总之是能用的。觉得不好,也可以加点代码处理一下。

提供一种用正则把 \\ 替换为 \hspace{...} 的尝试。

  • 因为需要同时影响目录和页眉,所以选择 \@chapter 作为修改的入口。
  • 正则的执行效率显著慢于 tex 中判断 if x in y 的典型方式(例如 keyval.sty\KV@split 的定义办法),但比较通用。

\chapter\@chapter 的展开逻辑

\chapter ::=
    <some staff>
    if next char == "*"       % \chapter*{title}
        \@schapter{title}
    else if next char == "["  % \chapter[short title]{title}
        \@chapter[short title]{title}
    else                      % \chapter{title}
        \@chapter[title]{title}
    end if

完整例子

\documentclass{ctexbook}
\usepackage[showframe]{geometry}
\usepackage{varwidth}


%% 设置 \chapter 的样式
\ctexset{
  chapter = {
    format += \centering,
    name = {第,回},
    nameformat += \kaishu,
    titleformat = \chaptertitleformat
  }
}

% 因为对 \@chapter 的修改,此处可恢复使用 varwidth 环境
\newcommand{\chaptertitleformat}[1]{%
  \CTEXifname{\begin{varwidth}[t]{.7\linewidth}#1\end{varwidth}}{#1}}


%% 增加 \chapter 对应目录项的 numwidth
\usepackage[titles]{tocloft}
\setlength{\cftchapnumwidth}{5\ccwd}


\ExplSyntaxOn
\makeatletter

%% 在 \chapter 的可选和必选参数的值相同时,把可选参数中的 "\\<任意个空白字符>" 
%% 替换为 "\hspace{0.5\ccwd}",间接影响在目录和页眉显示的章标题
\cs_set_eq:NN \@chapter:wn \@chapter

\cs_set_nopar:Npn \@chapter [#1]#2
  {
    \tl_set:Nn \l_tmpa_tl {#1}
    \tl_set:Nn \l_tmpb_tl {#2}
    \tl_if_eq:NNT \l_tmpa_tl \l_tmpb_tl
      { 
        \regex_replace_once:nnN { \s*\c{\x5C}\s* } { \c{hspace} \cB\{ 0\.5 \c{ccwd} \cE\} } \l_tmpa_tl
      }
    \@chapter:wn [\l_tmpa_tl] {\l_tmpb_tl}
  }

\makeatother
\ExplSyntaxOff

\begin{document}

\tableofcontents

\chapter{甄士隱夢幻識通靈\\ 賈雨村風塵懷閨秀}
\chapter{賈夫人仙逝揚州城\\ 冷子興演說榮國府}
\chapter{金陵城起複賈雨村\\ 榮國府收養林黛玉}
\addtocounter{chapter}{10}
\chapter{薄命女偏逢薄命郎\\ 葫蘆僧亂判葫蘆案}
\chapter{開生面夢演紅樓夢\\ 立新場情傳幻境情}
\chapter{賈寶玉初試雨雲情\\ 劉姥姥一進榮國府}

\end{document}

@lingr7
Copy link
Author

lingr7 commented Apr 27, 2020

感谢大家,目录已经修好了,已经release了一个全本红楼梦pdf文件
目录

第六回

@Liam0205
Copy link
Contributor

@lingr7 赞。项目地址在哪里?

@lingr7
Copy link
Author

lingr7 commented Apr 27, 2020

红楼梦排版的github仓库地址 https://github.com/lingr7/HongLouMeng-ZhiPingBen

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CJK/CTeX-collection Related to the CTeX macro collection. Layout/headings related to \part \chapter \section ...
Projects
None yet
Development

No branches or pull requests

4 participants