作者sppmg (sppmg)
看板LaTeX
标题[问题] \def展开时机
时间Tue Nov 22 22:31:35 2016
试了很久,实在不理解\def的展开时机。
\def\cmd{define} 之後,若将\cmd传入其他指令,
是会在使用到当下展开(进行运算)其定义,
还是说只有定义当下的值呢?(这好像不太合理)
我想要让目录中,一般章节的条目显示为:「一、 目录名」
但附录要维持「A、 附录名」
所以我在.cls中首先改写\thecontentslabel 设定
\gdef\thecontentslabel@chapter{
\ifx\@chapapp\appendixname % 判断 \appendix
\appendixname\thecontentslabel、
\else
\zhnumber{\thecontentslabel}、
\fi}
然後用titlesec套件的\titlecontents 改写目录的 chapter 条目格式
(下面的\tocStyle* 只是一些设定变数而已)
\titlecontents{chapter}[\tocNumSpaceC@Before]
{\tocStyleCheapterAbove}
{\tocStyleCheapterFont
\makebox[\tocNumSpaceC@Total][l]
{\thecontentslabel@chapter}}
{\tocStyleCheapterFont}
{\tocStyleCheapterFiller}
由於 \appendix 有这行
\gdef\@chapapp{\appendixname}
所以照预想,使用\appendix後,\thecontentslabel@chapter
应该要输出「\appendixname\thecontentslabel、」
但实际上它总是输出「\zhnumber{\thecontentslabel}、」
为什麽呢?
我也试了\renewcommand\appendix ,并包含
\gdef\thecontentslabel@chapter{\appendixname\thecontentslabel}
(\def \gdef均试过)
但这不会影响到已定义过的\titlecontents{chapter}
即使在重定义 \thecontentslabel@chapter 後,
再次使用完全一样的 \titlecontents{chapter} 进行设定也一样不会有任何效果。
但假若在 \titlecontents{chapter} 中直接使用
\appendixname\thecontentslabel
来取代 \thecontentslabel@chapter ,这样就可以了。
\titlecontents{chapter}[\tocNumSpaceC@Before]
{\tocStyleCheapterAbove}
{\tocStyleCheapterFont
\makebox[\tocNumSpaceC@Total][l]
{\appendixname\thecontentslabel}}
{\tocStyleCheapterFont}
{\tocStyleCheapterFiller}
--
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 114.32.188.151
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/LaTeX/M.1479825099.A.E11.html
咦!刚刚单独测试了下面这段
\makeatletter
\ifx\@chapapp\appendixname
{\Huge in appendix}
\else
{\Huge in normal}
\fi
\makeatother
竟然都是 in normal 。为什麽呢?
(这段是从
https://goo.gl/GKnuJU 来的)
※ 编辑: sppmg (114.32.188.151), 11/22/2016 23:09:25
1F:→ kerwinhui: 反过来用\chaptername好像可以? 11/23 12:24
2F:→ kerwinhui: 请无视上面的,已回了 11/23 13:50
感谢 kerwinhui 大,判断 \appendix 的解决了。
而\titlecontents 仍然无法使用的原因可能是因为 titletoc 中使用了
\edef 之类会定义期展开的指令。
我举个例子,参数我就用\titlecontents 内的,这样比较短。
\titlecontents{chapter}
[\Before] % TOC 条目左边界距(数字左方空白)
{\Above} % TOC 条目上方指令(添加空格)
{\withLabal} % TOC 中 \cheapter 的格式
{\withoutLabal} % TOC 中 \cheapter* 的格式
{\Filler} % 填充点线以及页码
假使我在首次使用时定义 \def\Above{} ,那之後我重定义
\def\Above{\addvspace{1em}}
这样不会影响到 \titlecontents ,但确实会影响上层指令。
如:
\def\a{1}
\def\b{\a}
\appto{\mainmatter}{ % etoolbox pkg.
mainmatter:
\def\a{2}
}
内文处
\b
\mainmatter
\b
将显示出
1
mainmatter:2
由此可知改指令定义是可以影响到上层。
加上若在\titlecontents 参数中包含判断式亦无效,
所以我猜它应该只有使用定义当下的值。(eg, \edef)
比较奇怪的是若我重定义(譬如\def\Above{...})後,
再使用完全一样的\titlecontents{...}{\Above}...
这样也是无效的。
难道说 LaTeX 有判断形式一样就不执行的功能?
看了titletoc.sty
% \titlecontents deals with concepts, not commands
\newcommand\titlecontents{%
\@ifstar{\ttl@contents{\z@}}%
{\ttl@contents{\@ne}}}
完全没有输入参数....
请问这要怎麽追踪....
※ 编辑: sppmg (114.32.188.151), 11/24/2016 12:18:07