LaTeX3 有一些很棒的概念和功能。其中之一是能够替换标记列表中的文本。这为创建模板提供了可能性,例如,html
模板被车把
此类模板存在的唯一原因是简化标记的输入,无论是人工输入还是计算机输入。考虑对多张图片进行标记的情况,例如以下图片:
标记可能会非常繁琐。以下形式的标记:
!!img amato!!
!!cap This is the first caption!!
会大大简化标记。!!img
是用于拾取图像和!!cap
拾取标题的标记。我使用 来!!
作为标记,但当然只要标记列表允许,就可以使用任何东西。后面的三个字母表示不同的标记,即img
用于图像、cap
用于标题。
以下是代码:
\documentclass{article}
\usepackage{expl3, graphicx,caption}
\begin{document}
\ExplSyntaxOn
\cs_set:Npn \putimage#1!!{%
\includegraphics[width=\linewidth]{./images/amato}%
}
\cs_set:Npn \putsomecaption#1!!{
\captionof{figure}{#1}
\par\endminipage\hfill
}
\tl_set:Nn \tempai {
\centering
!!img amato!!
!!cap This~is~the~first~caption!!
!!img amato!!
!!cap This~is~the~second~caption!!
!!img amato.jpg!!
!!cap This~is~the~third~caption!!
!!img amato!!
!!cap This~is~the~fourth~caption!!
!!img amato!!
!!cap This~is~the~fifth~caption!!
!!img amato!!
!!cap This~is~the~sixth~caption!!
}
\tl_replace_all:Nnn\tempai {!!img}{\minipage{3.6cm}\centering\putimage}
\tl_replace_all:Nnn\tempai {!!cap}{\putsomecaption}
\tl_use:N \tempai
\ExplSyntaxOff
\end{document}
我在合适的环境中转换上述代码时遇到了困难。有人能提供解决方案吗?假设有一个环境multimages
。
答案1
我不认为这是更简单的输入。但是,为了进一步追求 Manuel 的想法,这里有一个具有全功能选项的版本。
\documentclass{article}
\usepackage{showframe}
\usepackage{xparse,mwe,graphicx,caption,environ}
\ExplSyntaxOn
\NewEnviron{multiimages}[1][]
{
\keys_set:nn { yl/multiimages } { #1 }
\tl_use:N \l_yl_multi_start_tl
\dim_set:Nn \parindent { 0pt }
\skip_set:Nn \leftskip { 0pt plus 1fil }
\skip_set:Nn \rightskip { 0pt plus -1fil }
\skip_set:Nn \lineskip { \l_yl_multi_skip_skip }
\yl_multiimages:V \BODY
\tl_use:N \l_yl_multi_end_tl
}
\tl_new:N \l_yl_multi_start_tl
\tl_new:N \l_yl_multi_end_tl
\keys_define:nn { yl/multiimages }
{
env .choice:,
env/none .code:n =
\tl_set:Nn \l_yl_multi_start_tl { \par\addvspace{\topsep} }
\tl_set:Nn \l_yl_multi_end_tl { \par\addvspace{\topsep} },
env/figure .code:n =
\tl_set:Nn \l_yl_multi_start_tl { \__yl_multi_beginfigure:V \l_yl_multi_pos_tl }
\tl_set:Nn \l_yl_multi_end_tl { \end{figure} },
env .initial:n = none,
pos .tl_set:N = \l_yl_multi_pos_tl,
pos .initial:n = { htp },
outer .dim_set:N = \l_yl_multi_outer_dim,
outer .initial:n = 3.6cm,
inner .dim_set:N = \l_yl_multi_inner_dim,
inner .initial:n = 3.6cm,
skip .dim_set:N = \l_yl_multi_skip_skip,
skip .initial:n = \lineskip,
last .choice:,
last/fill .code:n =
\tl_set:Nn \l_yl_multi_last_tl { { \parfillskip=0pt\par } },
last/center .code:n =
\tl_set:Nn \l_yl_multi_last_tl { { \parfillskip=0pt plus 2fil\par } },
last .initial:n = fill,
}
\cs_new_protected:Npn \__yl_multi_beginfigure:n #1
{
\begin{figure}[#1]
}
\cs_generate_variant:Nn \__yl_multi_beginfigure:n { V }
\cs_new_protected:Npn \yl_multiimages:n #1
{
\tl_set:Nn \l_tmpa_tl { #1 }
\tl_remove_all:Nn \l_tmpa_tl { \par }
\tl_replace_all:Nnn \l_tmpa_tl { !!img ~ } { \__yl_multi_img:w }
\tl_replace_all:Nnn \l_tmpa_tl { !!cap ~ } { \__yl_multi_cap:w }
\tl_use:N \l_tmpa_tl \tl_use:N \l_yl_multi_last_tl
}
\cs_generate_variant:Nn \yl_multiimages:n { V }
\cs_new_protected:Npn \__yl_multi_img:w #1 !!
{
\begin{minipage}{\l_yl_multi_outer_dim}\centering
\includegraphics[width=\l_yl_multi_inner_dim]{ #1 }
}
\cs_new_protected:Npn \__yl_multi_cap:w #1 !!
{
\captionof{figure}{#1}
\end{minipage}\hspace{1pc plus 3pc}
}
\ExplSyntaxOff
\begin{document}
\begin{multiimages}[env=figure,pos=ht]
!!img example-image-a!!
!!cap This is the first caption!!
!!img example-image-a!!
!!cap This is the second caption!!
!!img example-image-a!!
!!cap This is the third caption!!
!!img example-image-a!!
!!cap This is the fourth caption!!
!!img example-image-a!!
!!cap This is the fifth caption!!
!!img example-image-a!!
!!cap This is the sixth caption!!
\end{multiimages}
\begin{multiimages}[last=center]
!!img example-image-a!!
!!cap This is the first caption!!
!!img example-image-a!!
!!cap This is the second caption!!
!!img example-image-a!!
!!cap This is the third caption!!
!!img example-image-a!!
!!cap This is the fourth caption!!
!!img example-image-a!!
!!cap This is the fifth caption!!
\end{multiimages}
\begin{multiimages}[last=center,env=figure,pos=p,inner=3cm,skip=10ex]
!!img example-image-a!!
!!cap This is the first caption!!
!!img example-image-a!!
!!cap This is the second caption!!
!!img example-image-a!!
!!cap This is the third caption!!
!!img example-image-a!!
!!cap This is the fourth caption!!
!!img example-image-a!!
!!cap This is the fifth caption!!
\end{multiimages}
\end{document}
答案2
\documentclass{article}
\usepackage{xparse,mwe,graphicx,caption,environ}
\NewEnviron{multiimages}{\processmultiimages{\BODY}}
\ExplSyntaxOn
\NewDocumentCommand \processmultiimages { m }
{ \par \centering \yl_multiimages:N #1 }
\cs_new_protected:Npn \yl_multiimages:N #1
{
\tl_set_eq:NN \l_tmpa_tl #1
\tl_remove_all:Nn \l_tmpa_tl { \par }
\tl_replace_all:Nnn \l_tmpa_tl { !!img ~ }
{ \begin{minipage}{3.6cm}\centering\multiimg }
\tl_replace_all:Nnn \l_tmpa_tl { !!cap ~ }
{ \multiimgcapt }
\tl_use:N \l_tmpa_tl
}
\NewDocumentCommand \multiimg { u{!!} }
{ \includegraphics[width=\linewidth]{ #1 } }
\NewDocumentCommand \multiimgcapt { u{!!} }
{ \captionof{figure}{#1} \par\end{minipage}\hfill }
\ExplSyntaxOff
\begin{document}
\begin{multiimages}
!!img example-image-a!!
!!cap This is the first caption!!
!!img example-image-a!!
!!cap This is the second caption!!
!!img example-image-a!!
!!cap This is the third caption!!
!!img example-image-a!!
!!cap This is the fourth caption!!
!!img example-image-a!!
!!cap This is the fifth caption!!
!!img example-image-a!!
!!cap This is the sixth caption!!
\end{multiimages}
\end{document}
但我不明白这到底有什么意义。我认为你应该看看的l3regex
正则表达式。
答案3
说实话,输入语法!!img zzz!!
似乎更多的比简单的 tex 语法复杂,\img{zzz}
但无论如何 expl3 当然对很多事情都很有用,但抓取环境主体然后在第二遍执行标记替换,最后在第三遍执行修改后的列表似乎效率低下,你可以简单地在一次传递中对其进行排版,例如
\documentclass{article}
\usepackage{graphicx,capt-of}
\def\multiimages{\centering\zzz}
\def\endmultiimages{\par}
\long\def\zz#1!img #2!!#3!!cap #4!!{%
\begin{minipage}{3.6cm}\centering
\includegraphics[width=\linewidth]{#2}%
\captionof{figure}{#4}%
\end{minipage}\hfill\zzz}
\def\zzs{!}
\def\zzp{\par}
\long\def\zzz#1{%
\def\tmp{#1}%
\ifx\tmp\zzs\expandafter\zz
\else
\ifx\tmp\zzp\expandafter\expandafter\expandafter\zzz
\else
\expandafter\expandafter\expandafter\end
\fi
\fi}
\begin{document}
\begin{multiimages}
!!img example-image-b!!
!!cap This is the first caption!!
!!img example-image-b!!
!!cap This is the second caption!!
!!img example-image-b!!
!!cap This is the third caption!!
!!img example-image-b!!
!!cap This is the fourth caption!!
!!img example-image-b!!
!!cap This is the fifth caption!!
!!img example-image-b!!
!!cap This is the sixth caption!!
\end{multiimages}
\end{document}
答案4
我的示例没有使用 expl3,而只是\replacestring
使用 OPmac。
\input opmac
\long\def\putimages#1{\def\tmpb{#1}%
\replacestrings{!!img }{\img}%
\replacestrings{!!cap }{\cap}%
\replacestrings{.jpg!!}{!!}%
\tmpnum=0
\hbox to\hsize{\tmpb}% three imeges per line
}
\def\img#1!!#2\cap#3!!{%
\ifnum\tmpnum=3 \egroup\bigskip\hbox to\hsize\bgroup \tmpnum=0 \fi
\advance\tmpnum by1
\vbox{\hsize=.3\hsize
\centerline{\picw=4cm \inspic #1.jpg }
\caption/f \emergencystretch=2em #3\par
}\hss
}
\def\thefnum{\the\fnum}
\putimages{
!!img amato!!
!!cap This is the first caption!!
!!img amato!!
!!cap This is the second caption!!
!!img amato.jpg!!
!!cap This is the third caption!!
!!img amato!!
!!cap This is the fourth caption!!
!!img amato!!
!!cap This is the fifth caption!!
!!img amato!!
!!cap This is the sixth caption!!
}
\bye
结果与您的 3x2 图片相同。
解释:第一遍将数据转换为:
\img amato!! \cap This is the first caption!! \par\img amato!! \cap ...
它存储在 中\tmpb
。然后\hbox{\tmpb}
创建 3x2 图片矩阵,因为\ifnum\tmpnum=3
然后\hbox
关闭 并且 新的\hbox
打开。