Comments (6)
谨防过度工程化。直接手写 \tl_const:Nn
也不怎么麻烦。
所以识别每个 item 有多少个
{}
就是关键。
\tl_count:n
\tl_count:n {abc}
=> 3\tl_count:n {{abc}}
=> 1\tl_count:n {a{bc}}
=> 2
看你想如何定义输入里不使用括号时,代码的行为。\whudefineconstant
要提供给用户使用吗?(样式/要求的多样性这么丰富吗?)
% !TeX program = xelatex
\documentclass{ctexart}
\ExplSyntaxOn
\NewDocumentCommand { \whudefineconstant } { m }
{
\clist_map_inline:nn { #1 }
{
\int_case:nnF { \tl_count:n {##1} }
{
{ 2 } { \__whu_define_constant_aux:nn ##1 }
{ 3 } { \__whu_define_constant_aux:nnn ##1 }
% more cases
}
% raise error
{ }
}
}
\cs_new_protected:Nn \__whu_define_constant_aux:nn
{
\tl_const:cn { c__whu_constant_ #1 _tl } {#2}
}
\cs_new_protected:Npn \__whu_define_constant_aux:nnn #1#2#3
{
\tl_const:cn { c__whu_constant_ #1 _tl } {#2}
\tl_const:cn { c__whu_constant_ #1 _en_tl } {#3}
}
\ExplSyntaxOff
\begin{document}
\whudefineconstant{
{acknowledgement}{致\quad 谢}{ACKNOWLEDGEMENT},
{abstract}{摘\qquad 要}
}
\newcommand{\WhuPrintConstant}[1]{%
\par\noindent
Constant ``\textbf{#1}'':
\ifcsname c__whu_constant_#1_tl\endcsname
\\\null\qquad (cn) \UseName{c__whu_constant_#1_tl}
\ifcsname c__whu_constant_#1_en_tl\endcsname
\\\null\qquad (en) \UseName{c__whu_constant_#1_en_tl}
\fi
\else
\\\null\qquad ! Undefined
\fi
\par
}
\WhuPrintConstant{random}
\WhuPrintConstant{acknowledgement}
\WhuPrintConstant{abstract}
\end{document}
参数一多,记忆参数的位置/顺序会带来负担,也许 key-value 形式更好。
一个 key-value 的尝试
% !TeX program = xelatex
\documentclass{ctexart}
\ExplSyntaxOn
\NewDocumentCommand { \whudefineconstant } { m }
{
\keys_set:nn { whu/constant } {#1}
}
\cs_new_protected:Nn \__whu_define_constant_family:n
{
\keys_define:nn { whu/constant }
{
#1/中文正文 .code:n =
{ \__whu_set_constant:nnn {#1} { \l_keys_key_str } {##1} }
, #1/中文目录 .code:n =
{ \__whu_set_constant:nnn {#1} { \l_keys_key_str } {##1} }
, #1/英文正文 .code:n =
{ \__whu_set_constant:nnn {#1} { \l_keys_key_str } {##1} }
, #1/英文目录 .code:n =
{ \__whu_set_constant:nnn {#1} { \l_keys_key_str } {##1} }
, #1 .code:n =
{
\keys_set:nn { whu/constant/#1 } { ##1 }
\bool_lazy_and:nnT
{ \__whu_constant_if_exist_p:nn {#1} {中文正文} }
{ ! \__whu_constant_if_exist_p:nn {#1} {中文目录} }
{
\__whu_set_constant:nnn {#1} {中文目录}
{ \__whu_use_constant:nn {#1} {中文正文} }
}
\bool_lazy_and:nnT
{ \__whu_constant_if_exist_p:nn {#1} {英文正文} }
{ ! \__whu_constant_if_exist_p:nn {#1} {英文目录} }
{
\__whu_set_constant:nnn {#1} {英文目录}
{ \__whu_use_constant:nn {#1} {英文正文} }
}
}
}
}
\__whu_define_constant_family:n {致谢}
\__whu_define_constant_family:n {摘要}
\__whu_define_constant_family:n {啥啥}
\cs_new_protected:Nn \__whu_set_constant:nnn
{
\tl_const:cn { c__whu_constant_ #1/#2 _tl } {#3}
}
\cs_new_protected:Nn \__whu_use_constant:nn
{
\tl_use:c { c__whu_constant_ #1/#2 _tl }
}
\cs_new_protected:Nn \__whu_get_constant_csname:nn
{
c__whu_constant_ #1/#2 _tl
}
\prg_new_conditional:Nnn \__whu_constant_if_exist:nn { p , T , F , TF }
{
\tl_if_exist:cTF { \__whu_get_constant_csname:nn {#1} {#2} }
\prg_return_true:
\prg_return_false:
}
\ExplSyntaxOff
\begin{document}
\whudefineconstant{
致谢={
英文正文=ACKNOWLEDGEMENT,
英文目录=Acknowledgement,
中文正文=致\quad 谢,
中文目录=致谢,
},
摘要={
中文正文=摘\qquad 要,
英文正文=Abstract,
},
啥啥={
中文正文=啥啥,
},
}
\ExplSyntaxOn
\cs_new_protected:Npn \WhuPrintConstant #1
{
\par\noindent
\textbf{#1}
\__whu_print_constant_aux:nn {#1} {中文正文}
\__whu_print_constant_aux:nn {#1} {中文目录}
\__whu_print_constant_aux:nn {#1} {英文正文}
\__whu_print_constant_aux:nn {#1} {英文目录}
\par
}
\cs_new_protected:Nn \__whu_print_constant_aux:nn
{
\\\null\qquad (#2):~
\cs_if_exist_use:cF { c__whu_constant_ #1/#2 _tl } { \fbox{未定义} }
}
\ExplSyntaxOff
\WhuPrintConstant{致谢}
\WhuPrintConstant{摘要}
\WhuPrintConstant{啥啥}
\end{document}
输入
\whudefineconstant{
致谢={
英文正文=ACKNOWLEDGEMENT,
英文目录=Acknowledgement,
中文正文=致\quad 谢,
中文目录=致谢,
},
摘要={
中文正文=摘\qquad 要,
英文正文=Abstract,
},
啥啥={
中文正文=啥啥,
},
}
我还是觉得,这有过度工程化的嫌疑。把
\addtocontentsline...
\chapter*[...]{...}
暴露给用户不是很坏的方案。
from forum.
谨防过度工程化。直接手写
\tl_const:Nn
也不怎么麻烦。
我的问题,我没有想过度工程化,只是我不知道(忘记了)还可以用 \tl_count:
来计算 {}
啥的。
\whudefineconstant
要提供给用户使用吗?(样式/要求的多样性这么丰富吗?)
这个命令其实更多情况下都是开发层的使用,但也计划要提供给用户,让用户可以根据一些特殊情形微调,不过可能用的不多。
参数一多,记忆参数的位置/顺序会带来负担,也许 key-value 形式更好。
感谢慕子!这个键值的处理挺不错的,我学习一下。
话说工程化有啥问题吗?不太懂,我写的时候也没想太多。我写模板可能更多只是为了提升自己的水平 hhhh,在写的过程中学一些新东西。
from forum.
\whudefineconstant
要提供给用户使用吗?(样式/要求的多样性这么丰富吗?)这个命令其实更多情况下都是开发层的使用,但也计划要提供给用户,让用户可以根据一些特殊情形微调,不过可能用的不多。
开发者设置一次、用户在特殊情况下再设置一次,就会修改(已初始化的)常量的值,于是很可能应该按局部或全局变量来命名(\l__whu_...
或 \g__whu_...
)。与 nju-lug/NJUThesis#240 类似。
我后来想到,除了把每一条设置都储存在单独的 tl 里,还可以用 prop/seq 储存(\g__whu_abstract_titles_prop
),虽然它们都是 O(n) 的,但这里 n <= 4。长度确定的情况下,存成 \tl_gset:Nn \g__whu_abstract_titles_tl {{<cn>}{<cn-toc>}{<en>}{<en-toc>}}
的 tuple 形式也可以,然后用 \use_(i|ii|iii|iv):nnnn
取用。
from forum.
开发者设置一次、用户在特殊情况下再设置一次,就会修改(已初始化的)常量的值,于是很可能应该按局部或全局变量来命名(
\l__whu_...
或\g__whu_...
)。与 nju-lug/NJUThesis#240 类似。
如果在模板里的话,l
和 g
有什么不同的使用场景吗
from forum.
设计上,声明总是全局的,然后总是局部赋值 \l_xxx_<type>
,如 set
、put_right
、clear
;总是全局赋值\g_xxx_<type>
,如 gset
、gput_right
、gclear
。这么做能避免影响 save stack(可以在 texdoc texbytopic
里直接搜 "save stack")。
实现上,以上只是软性约束,也就是 \tl_gset:Nn \l_tmpa_tl {...}
不会报错。开启 \debug_on:n { check-declarations }
后,对 c/l/g
不匹配的使用会报错(目前覆盖不全,会逐渐补全)。
功能上,如果需要某些设置局部生效,如 {\mysetup{<new settings>} new settings applied} old settings restored
就适合用 l
。反之如果需要在当前分组结束后仍然生效,就适合用 g
。其他时候可以任选。我的印象是,传统的设置项,内部大多用的局部赋值。
from forum.
好的,感谢。我感觉这个常量还是适合用 g
一些。
from forum.
Related Issues (20)
- error
- 超页面问题 HOT 2
- 如何让“摘要”出现在目录中,但不要显示后面的“......页码” HOT 10
- 空白页控制问题 HOT 10
- 命令可选参数键值的值为 LaTeX3 的 cs 的可行性 HOT 3
- 关于 `ntheorem` 和 `amsthm` 中的 `\qedhere` 优化问题 HOT 5
- 给特殊符号设置xeCJKsetcharclass,并打开CJKmath,特殊符号仍不能直接用于数学模式 HOT 9
- `\keys_define:nn` 中 `.initial:n` 失效的问题 HOT 15
- `xampl.bib` 无法用于 `gbt7714` 包和 `natbib` 的 `authoryear` 模式吗? HOT 8
- 安装ctex时报错 HOT 1
- 如何在每一章的指定位置插入边注 HOT 8
- \newtheorem和amsthm冲突问题 HOT 7
- 如何优雅的让行内公式换行? HOT 4
- datetime2宏包如何在使用xelatex编译时取当前详细时间(秒数+时区)并转换为东八区 HOT 13
- 采用 listings 宏包排代码,代码里的汉字之间有空格 HOT 2
- Ctex如何设置页边距,另外,类似论文中的项目资助的脚注 如何实现? HOT 5
- 我想把公式编号连带编号两边的括号 的字体 都设置为Times New Roman,但 左边那个括号的字体就是改不了 HOT 6
- 求救帖,被一个英文文章翻译任务逼疯了,求大佬协助完成一个模板,我完成了大概有6成,剩下的搞不定了 HOT 6
- 关于ulem包与xeCJKfntef包连用hyperref包进行超链接下划线的疑惑 HOT 11
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from forum.