![带有子标签的环境](https://linux22.com/image/328806/%E5%B8%A6%E6%9C%89%E5%AD%90%E6%A0%87%E7%AD%BE%E7%9A%84%E7%8E%AF%E5%A2%83.png)
是否有可能创建一个可用环境……?
\begin{myEnvironment}
\{title}[This is a title]
\{year}[2016]
\{content}[Lorem ipsum dolor sit amet, consectetur adipisci elit,
sed eiusmod tempor incidunt ut labore et dolore magna aliqua. Ut
enim ad minim veniam, quis nostrum exercitationem ullam corporis
suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur.]
\end{myEnvironment}
...或者这样...?
\begin{myEnvironment}
\{title} This is a title
\{year} 2016
\{content} Lorem ipsum dolor sit amet, consectetur adipisci elit,
sed eiusmod tempor incidunt ut labore et dolore magna aliqua. Ut
enim ad minim veniam, quis nostrum exercitationem ullam corporis
suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur.
\end{myEnvironment}
...或者类似的东西?
如果我愿意,我可以省略一个字段(例如title
)。
输出应该是这样的(例如):
- 标题:这是一个标题
- 年:2016
Lorem ipsum dolor sit amet,consectetur adipisci elit,sed eiusmod tempor incidunt ut labore et dolore magna aliqua。出于最低限度的考虑,我们练习用自己的身体来做苦工,除非我们用货物来做苦工。
答案1
是的,这是可能的,并且具有友好的用户级语法。
\documentclass{article}
\usepackage{xparse}
\ExplSyntaxOn
\NewDocumentEnvironment{myenv}{m} % <--- choose a better name
{
% process the keys
\keys_set:nn { myenv } { #1 }
% print them
\par\addvspace{\topsep}
\noindent\makebox[4em][l]{\textbullet\ \textbf{Title:}}~\l_myenv_title_tl
\\*
\noindent\makebox[4em][l]{\textbullet\ \textbf{Year:}}~\l_myenv_year_tl
\\*
\ignorespaces
}
{
\par\addvspace{\topsep}
}
% define the keys
\keys_define:nn { myenv }
{
title .tl_set:N = \l_myenv_title_tl,
year .tl_set:N = \l_myenv_year_tl,
title .value_required:n = true,
year .value_required:n = true,
}
\ExplSyntaxOff
\begin{document}
\begin{myenv}{
title = This is a title,
year = 2016,
}
Lorem ipsum dolor sit amet, consectetur adipisci elit,
sed eiusmod tempor incidunt ut labore et dolore magna
aliqua. Ut enim ad minim veniam, quis nostrum exercitationem
ullam corporis suscipit laboriosam, nisi ut aliquid ex ea
commodi consequatur.
\end{myenv}
\end{document}