可以使用 cleveref
宏包。
主要的配置命令如下:
% \usepackage{hyperref}
% \usepackage{cleveref} % 注意顺序
% 修改 autorefname,也方便在后面使用
\def\figureautorefname{Fig.}
\def\figureautorefnames{Figs.}
\crefformat{figure}{#2\figureautorefname~#1#3}
\crefmultiformat{figure}
{#2\figureautorefnames~#1#3}
{ and~#2#1#3}
{, #2#1#3}
{ and~#2#1#3}
\crefrangeformat{figure}{#3\figureautorefnames~#1#4 to~#5#2#6}
详细用法见说明文档。
完整的例子:
\documentclass{article}
\usepackage[colorlinks]{hyperref}
\usepackage{cleveref}
\def\figureautorefname{Fig.}
\def\figureautorefnames{Figs.}
\crefformat{figure}{#2\figureautorefname~#1#3}
\crefmultiformat{figure}
{#2\figureautorefnames~#1#3}
{ and~#2#1#3}
{, #2#1#3}
{ and~#2#1#3}
\crefrangeformat{figure}{#3\figureautorefnames~#1#4 to~#5#2#6}
\begin{document}
Hello, \LaTeXe.
\begin{figure}[h]
\caption{FIGURE ONE}\label{fig:1}
\centering\fbox{FIGURE ONE}
\end{figure}
\begin{figure}[h]
\caption{FIGURE TWO}\label{fig:2}
\centering\fbox{FIGURE TWO}
\end{figure}
\begin{figure}[h]
\caption{FIGURE THREE}\label{fig:3}
\centering\fbox{FIGURE THREE}
\end{figure}
reference: \ref{fig:1} \& \ref{fig:2} \& \ref{fig:3}
autoref: \autoref{fig:1} \& \autoref{fig:2} \& \autoref{fig:3}
cleveref: \cref{fig:1} \& \cref{fig:2} \& \cref{fig:3}
multi cref: \cref{fig:1,fig:2} \& \cref{fig:1,fig:2,fig:3} \& \cref{fig:1,,fig:2,,fig:3}
---------------------------
\crefmultiformat{figure}
{\figureautorefnames~#2#1#3}
{ and~#2#1#3}
{, #2#1#3}
{ and~#2#1#3}
\crefrangeformat{figure}{\figureautorefnames~#3#1#4 to~#5#2#6}
multi cref: \cref{fig:1,fig:2} \& \cref{fig:1,fig:2,fig:3} \& \cref{fig:1,,fig:2,,fig:3}
\end{document}
编译不成功的原因是
\newtcbtheorem
不会展开其参数;\setkeys
以及几乎所有的键设置命令(\keys_set:nn
、\pgfkeys
)都是不可扩展的。(也有一部分命令是可扩展的,如 \keyval_parse:nnn
、expkv
宏包的部分命令。)首先要说明的是,LaTeX3 不依赖任何其它宏包,甚至都不需要是 LaTeX。以及 l3keys
也不依赖 l3prop
,这二者是独立的。键值设置一般使用 l3keys
,l3prop
使用得较少。
要想成功编译,首先必须解析传入的键值,然后将其传给 \newtcbtheorem
。
可用的方法有很多,但是既然使用了 tcolorbox
,就使用 pgfkeys
来实现吧。
首先定义几个辅助命令与句柄(handler):
\makeatletter
\ExplSyntaxOn
\cs_new_nopar:Npn \zt@appto@clist #1#2 { \clist_put_right:Nn #2 {#1} }
\cs_new_nopar:Npn \zt@preto@clist #1#2 { \clist_put_left:Nn #2 {#1} }
\ExplSyntaxOff
\pgfkeys{
% {<prefix list>} {<clist>},把 <prefix list> 放到 <clist> 之前,<clist> 是一个 list 宏
/handlers/.prefix to clist/.code 2 args=%
\pgfkeysdef{\pgfkeyscurrentpath}{\zt@preto@clist{#1}{#2}},
% {<append list>} {<clist>}
/handlers/.append to clist/.code 2 args=%
\pgfkeysdef{\pgfkeyscurrentpath}{\zt@appto@clist{#1}{#2}},
}
\tcbset{
zljTheoStyle/.style={}, % 你自己的代码
}
% 这个命令类似于 \tikzset、\tcbset,它的形式是 \ztpgfset{<module>}{<key-val>}
\def\ztpgfset#1{\pgfqkeys{zt/#1}}
\NewDocumentCommand\ztset{O{}}
{
\def\zt@tmplist{}% 这个命令用于保存解析的键值
\ztpgfset{tcb}{#1} % 这个命令用于解析传入的键值参数,使用 tcb 模块
\def\zt@tmp{\newtcbtheorem{zljtest}{name}}
\expandafter\zt@tmp\expandafter{\zt@tmplist,zljTheoStyle,}{zljtest} % 必须展开解析得到的键值
}
现在就可以定义自己的键了,把它们放到 tcb 模块:
\ztpgfset{tcb}{
color/.append to clist={colbacktitle={#1}}{\zt@tmplist},% 把 "colbacktitle={#1}" 放到 \zt@tmplist 之后,效果和 "color/.code=\zt@appto@clist{#1}{\zt@tmplist}" 一样
color/.default=red,% 默认值为 red
cf/.append to clist={colframe=#1}{\zt@tmplist},% 把 "colframe=#1" 放到 \zt@tmplist 之后
cf/.default=yellow!80!black,
}
\makeatother
一个完整的例子:
\documentclass{ctexart}
\usepackage[most]{tcolorbox}
\makeatletter
\ExplSyntaxOn
\cs_new_nopar:Npn \zt@appto@clist #1#2 { \clist_put_right:Nn #2 {#1} }
\cs_new_nopar:Npn \zt@preto@clist #1#2 { \clist_put_left:Nn #2 {#1} }
\ExplSyntaxOff
\pgfkeys{
/handlers/.prefix to clist/.code 2 args=%
\pgfkeysdef{\pgfkeyscurrentpath}{\zt@preto@clist{#1}{#2}},
/handlers/.append to clist/.code 2 args=%
\pgfkeysdef{\pgfkeyscurrentpath}{\zt@appto@clist{#1}{#2}},
}
\tcbset{
zljTheoStyle/.style={}, % 你自己的代码
}
\def\ztpgfset#1{\pgfqkeys{zt/#1}}
\NewDocumentCommand\ztset{O{}}
{
\def\zt@tmplist{}% 这个命令用于保存解析的键值
\ztpgfset{tcb}{#1} % 这个命令用于解析传入的键值参数,使用 tcb 模块
\def\zt@tmp{\newtcbtheorem{zljtest}{name}}
\expandafter\zt@tmp\expandafter{\zt@tmplist,zljTheoStyle,}{zljtest}
}
\ztpgfset{tcb}{
color/.append to clist={colbacktitle={#1}}{\zt@tmplist},% 把 "colbacktitle={#1}" 放到 \zt@tmplist 之后
color/.default=red,% 默认值为 red
cf/.append to clist={colframe=#1}{\zt@tmplist},% 把 "colframe=#1" 放到 \zt@tmplist 之后
cf/.default=yellow!80!black,
}
\makeatother
\begin{document}
\ztset[color=blue,cf]
\begin{zljtest}{}{}
测试基
\end{zljtest}
\end{document}
在正常的 LaTeX3
环境(类代码,字符码等)下是一样的。
\cs_new:cn
实际上是 \exp_args:Nc \cs_new:Nn
,\exp_args:Nc <#1> <#2>
是 \expandafter<#1>\csname <#2>\endcsname
。
所以实际上 \cs_new:cn
就是先把第一个参数变成一个控制序列,然后再把 \cs_new:Nn
放在这个控制序列的前面。
难点在于既要使用 alignat
环境(包含编号),又要有大括号,又要跨括号对齐。任意两点其实都不算难,但是三点都要实现就比较麻烦了。这里提供两种并不优雅的解决方案。
一个是使用 array
环境。
% \usepackage{array,multirow}
{\renewcommand\arraystretch{1.3}\[
\begin{array}{r@{} *{2}{r@{}>{{}}c<{{}}@{}} l@{\qquad}r} %% @{} 为了去除间距
\multirow{2}*{$\biggl\{\biggr.$}& x & + & 3y & = & 11 & (1) \\
& 2x & - & y & = & -9 & (2) \\
\multirow{2}*{result$\biggl\{\biggr.$} & x & = & x_1 &&& (1') \\
& y & = & y_1 &&& (2')
\end{array}
\]}
效果如下:
这里的大括号是对齐的
不过编号的位置需要设置,这里使用一个 \quad
。
另一个就是使用 empheq
宏包的 empheq
环境,该宏包属于 mathtools
的一部分,(详细用法查看其参考文档)。
实际上,不要求括号处对齐是比较容易实现的:
% \usepackage{amsmath,empheq}
\begin{empheq}[left={ }\empheqlbrace]{alignat*=2}
x &+{}& 3y &= 11, \tag{1} \\
2x &-{}& y &= -9 \tag{2}
\end{empheq}
\begin{empheq}[left=\llap{result}\empheqlbrace]{align*}
x&=x_1, \tag{1'} \\
y&=y_1 \tag{2'}
\end{empheq}
如果在括号处对齐则需要半手动调整间距了:
% \usepackage{amsmath,empheq}
\newdimen\tmpadim
\newdimen\tmpbdim
%% 这两个是为了计算宽度
\settowidth\tmpadim{$
\begin{alignedat}{2}
x &+{}& 3y &= 11, \\
2x &-{}& y &= -9
\end{alignedat}
$}
\settowidth\tmpbdim{$
\begin{aligned}
x &= x_1, \\
y &= y_1
\end{aligned}
$}
\begin{empheq}[left={ }\empheqlbrace]{alignat*=2}
x &+{}& 3y &= 11, \tag{1}\label{eq:1} \\
2x &-{}& y &= -9 \tag{2}
\end{empheq}
\begin{empheq}[left=\llap{result}\empheqlbrace]{align*}
x&=x_1, \hspace{\dimexpr\tmpadim-\tmpbdim} \tag{1'}\label{eq:1'} \\ % 插入一个间距
y&=y_1 \tag{2'}
\end{empheq}
\eqref{eq:1} and \eqref{eq:1'}
括号处是对齐的:
我们知道,如果一个宏是使用如下方式定义的:
\def\foo#1\T{something #1 else...}
那么在使用该宏时必须使用 \T
作为(第一个)参数的分隔符,也就是必须使用
\foo balabala\T
如若没有 \T
,TeX 将会报错。
你的代码就是这种错误。
因为使用了 \cs_new:Npn \__tk_before_slashsearch:w #1 / #2 \s_stop
,所以参数分隔符 /
和 \s_stop
是必须给出的。
但是 \clist_map_function:NN <clist> <func>
所做的仅仅是把 <clist>
的 <item>
置于 <func>
之后,也就是
\__tk_before_slashsearch:w <item>
这样 \s_stop
是没有的,所以必须通过一个辅助函数给出,也就是原来 \__tk_slashed_items:n
的作用之一。
因为这里的 \tk_iteratesearch:N
必须可扩展,所以不能使用 \clist_map_inline:Nn
。
没懂你想干什么。但是呢 LaTeX3 提供了一个 \cs_to_str:N
命令,用于输出控制序列的名字。
\documentclass{article}
\ExplSyntaxOn
\cs_new_protected_nopar:Npn \csusewithname #1
{
\cs_to_str:N #1
#1
}
\ExplSyntaxOff
\begin{document}
\csusewithname\section{sec 1}
\csusewithname\label{lab:1}
\ref{lab:1}
\end{document}
出现这种情况是因为 extarrows
宏包内部使用了 amsmath
宏包的 \ext@arrow
命令,该命令的第 #6
、#7
参数对应于 \xlongrightarrow[<#6>]{<#7>}
。只是这 #6
、#7
会被使用两次,一次用于测量盒子的宽度,另一次则用于输出。(见 \ext@arrow
的定义,位于 amsmath.sty
)
知道这个就容易修改了:
\makeatletter
\newif\ifext@arrow@measuring@
\let\saved@ext@arrow\ext@arrow % 保存原始定义
\def\ext@arrow#1#2#3#4#5#6#7{%
\mathrel{\mathop{%
\setbox\z@\hbox{#5\displaystyle}%
\ext@arrow@measuring@true% 增加一个
\setbox\tw@\vbox{\m@th
\hbox{$\scriptstyle\mkern#3mu{#6}\mkern#4mu$}%
\hbox{$\scriptstyle\mkern#3mu{#7}\mkern#4mu$}%
\copy\z@
}%
\hbox to\wd\tw@{\unhbox\z@}}%
\ext@arrow@measuring@false% 增加一个,实际上是不需要的,由于 \mathop 构建一个组
\limits
\@ifnotempty{#7}{^{\if0#1\else\mkern#1mu\fi
#7\if0#2\else\mkern#2mu\fi}}%
\@ifnotempty{#6}{_{\if0#1\else\mkern#1mu\fi
#6\if0#2\else\mkern#2mu\fi}}}%
}
% 直接写 \def\arrowmeasuring#1#2{\ifext@arrow@measuring#1\else#2\fi} 也可以
\def\arrowmeasuring{\ifext@arrow@measuring@\expandafter\@firstoftwo\else\expandafter\@secondoftwo\fi}
\def\fake@footnotemark{\textsuperscript{\the\numexpr\value{footnote}+1\relax}}
\def\longrightmark{\arrowmeasuring{\fake@footnotemark}{\footnotemark}} % 增加一个判断
\makeatother
然后呢就可以使用
\begin{tabular}{cc}
a & b \\
$\xlongrightarrow{x + y \longrightmark}$ & d
\end{tabular}
\footnotetext{note1}
或者
\begin{table}[htbp]
\begin{tabular}{cc}
a & b \\
$\xlongrightarrow{x + y \arrowmeasuring{\csname fake@footnotemark\endcsname}{\tablefootnote{note2}}}$ & d
\end{tabular}
\end{table}
都能正确生成了。
一个更好的办法是使用 etoolbox
宏包的 \patchcmd
命令,而不直接重定义 \ext@arrow
。
%\usepackage{etoolbox}
\patchcmd\ext@arrow{\mathop}{\ext@arrow@measuring@true\mathop}{}{}
% 不在 \mathop 之内,必须加上
\patchcmd\ext@arrow{\limits}{\ext@arrow@measuring@false\limits}{}{}
完整的例子:
\documentclass[UTF8,11pt]{ctexart}
\usepackage{hyperref}
\usepackage{extarrows}
\usepackage{tablefootnote}
\usepackage{etoolbox}
\makeatletter
\newif\ifext@arrow@measuring@
\let\saved@ext@arrow\ext@arrow % 保存原始定义
\def\fake@footnotemark{\textsuperscript{\the\numexpr\value{footnote}+1\relax}}
\patchcmd\ext@arrow{\mathop}{\ext@arrow@measuring@true\mathop}{}{}
\patchcmd\ext@arrow{\limits}{\ext@arrow@measuring@false\limits}{}{}
\def\arrowmeasuring#1#2{\ifext@arrow@measuring@#1\else#2\fi}
\def\longrightmark{\arrowmeasuring{\fake@footnotemark}{\footnotemark}}
\def\amsmeasuring#1#2{\ifmeasuring@#1\else#2\fi}
\makeatother
\begin{document}
\begin{tabular}{cc}
a & b \\
$\xlongrightarrow{x + y \longrightmark}$ & d
\end{tabular}
\footnotetext{note1}
\begin{table}[htbp]
\begin{tabular}{cc}
a & b \\
$\xlongrightarrow{x + y \arrowmeasuring{\csname fake@footnotemark\endcsname}{\tablefootnote{note2}}}$ & d
\end{tabular}
\end{table}
\end{document}
对 \ext@arrow
的修改应该是不会影响正常使用的。这对于所有使用 \ext@arrow
定义的命令都是有效的,如 \xrightarrow
等。
另一个方案见:https://tex.stackexchange.com/questions/335968/skipped-footnote-numbers-in-math-mode。
不建议在数学模式下使用 footnote
应该是vscode的配置问题。LaTeX Workshop 配置有点迷。
宏包本身没有冲突。
而且,listing
和 minted
使用一个就好了。
newtxmath
宏包应该没有 \wideparen
命令。
为了解决这一点,只要在 newtxmath
宏包加载前使用 \let\widering\relax
取消 \widering
的定义即可,因为 newtxmath
也定义了同样的命令。
\usepackage{yhmath}%为了使用该宏包里的圆弧帽命令wideparen{}
\let\widering\relax
\usepackage{newtxtext}
\usepackage{newtxmath}
cases
环境无法做到这一点,本质上,cases
环境只是两列的 array
环境。
考虑使用 amsmath
宏包的 alignat
环境(的 inline 版 alignedat
)。括号使用 \left\{
\right.
(cases
环境也是使用该方法实现)。
四.已知线性方程组 $\left\{\begin{alignedat}{3}
% 这里的 {} 是为了保持+的间距,并不优雅
3\lambda &x_{1} +{}&(2\lambda +1) &x_{2} +{}&(\lambda+1) x_{3} &=\lambda\\
(2\lambda+1) &x_{1} +{}&(2\lambda +1) &x_{2} +{}&(\lambda-2) x_{3} &=\lambda+1\\
(4\lambda-1) &x_{1} +{}& 3\lambda &x_{2} +{}& 2\lambda x_{3} &=1
\end{alignedat}\right.$.
求$\lambda$为何值时,方程组无解,有唯一解,有无穷多解?
\IfBooleanTF{#1}
{}
{
\int_add:Nn \l_counter_tl {1}
{
\heiti
\color{\tl_use:N \l_color_tl}
\Large
\int_use:N \l_counter_tl
}
}
直接写就可以了,\int_...
会自动展开。当然也可以使用 \exp_after:wN \int_... \l_counter_tl
,不过 \exp_after:wN
是多余的。
你也可以在 \str_case:nn
中使用 \tl_set_eq:NN \l_counter_tl \..._int
。这是因为 \tl_set_eq:NN
就是 \let
。
另外 \color
中的 \tl_use:N
是不必要的,它会自动扩展。(而且 \tl_ue:N
实际上只是检查了一下命令是否存在)
(而 \color_select:n
则必须首先扩展。)
\int_add:Nn .. 1
可简写为 \int_incr:N ..
,并且速度更快。
(LaTeX3的命名规范,变量:\⟨scope⟩_⟨module⟩_⟨description⟩_⟨type⟩
,函数:\⟨module⟩_⟨description⟩:⟨arg-spec⟩
,module
和description
应该用_
分隔,module
一般是必须的。)
使用 hyperref
宏包后,\newlabel
的第二个参数需要5个值。
\makeatletter
\bgroup\catcode`\ =9 \endlinechar=-1 % 简单模拟LaTeX3环境
\newcounter{probdaily} % \int_new:N \g_probdaily_counter_int
\AfterLastShipout{
\immediate\write\@auxout
{\string\newlabel{ProbdailyTotal}
{
{\the\c@probdaily} % \int_use:N \g_probdaily_counter_int
{}
{}
{}
{}
}
}
}
\egroup
\makeatother
在 LaTeX2e 2020-10-01
版本之后,ateveryend
宏包是不需要的,\AfterLastShipout
已原生提供。
\documentclass{article}
\usepackage{hyperref}
\makeatletter
\bgroup\catcode`\ =9 \endlinechar=-1
\newcounter{probdaily}
\AfterLastShipout{
\immediate\write\@auxout
{\string\newlabel{ProbdailyTotal}
{
{\the\c@probdaily}
{}
{}
{}
{}
}
}
}
\egroup
\makeatother
\begin{document}
Hello, \LaTeXe.
\stepcounter{probdaily}
\stepcounter{probdaily}
\ref{ProbdailyTotal} = 3
\stepcounter{probdaily}
\end{document}
问 请问有没有方法能获取到准确的行高