Giter Club home page Giter Club logo

Comments (6)

muzimuzhi avatar muzimuzhi commented on August 12, 2024

谨防过度工程化。直接手写 \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}

image

参数一多,记忆参数的位置/顺序会带来负担,也许 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,
  },
  啥啥={
    中文正文=啥啥,
  },
}

存储的信息
image

我还是觉得,这有过度工程化的嫌疑。把

\addtocontentsline...
\chapter*[...]{...}

暴露给用户不是很坏的方案。

from forum.

xkwxdyy avatar xkwxdyy commented on August 12, 2024

谨防过度工程化。直接手写 \tl_const:Nn 也不怎么麻烦。

我的问题,我没有想过度工程化,只是我不知道(忘记了)还可以用 \tl_count: 来计算 {} 啥的。

\whudefineconstant 要提供给用户使用吗?(样式/要求的多样性这么丰富吗?)

这个命令其实更多情况下都是开发层的使用,但也计划要提供给用户,让用户可以根据一些特殊情形微调,不过可能用的不多。

参数一多,记忆参数的位置/顺序会带来负担,也许 key-value 形式更好。

感谢慕子!这个键值的处理挺不错的,我学习一下。

话说工程化有啥问题吗?不太懂,我写的时候也没想太多。我写模板可能更多只是为了提升自己的水平 hhhh,在写的过程中学一些新东西。

from forum.

muzimuzhi avatar muzimuzhi commented on August 12, 2024

\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.

xkwxdyy avatar xkwxdyy commented on August 12, 2024

开发者设置一次、用户在特殊情况下再设置一次,就会修改(已初始化的)常量的值,于是很可能应该按局部或全局变量来命名(\l__whu_...\g__whu_...)。与 nju-lug/NJUThesis#240 类似。

如果在模板里的话,lg 有什么不同的使用场景吗

from forum.

muzimuzhi avatar muzimuzhi commented on August 12, 2024

设计上,声明总是全局的,然后总是局部赋值 \l_xxx_<type> ,如 setput_rightclear;总是全局赋值\g_xxx_<type>,如 gsetgput_rightgclear。这么做能避免影响 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.

xkwxdyy avatar xkwxdyy commented on August 12, 2024

好的,感谢。我感觉这个常量还是适合用 g 一些。

from forum.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.