我正在尝试创建一个列表环境,以某种方式标记每个项目。老实说,我可以手动完成,但我想了解有关环境创建过程的更多信息,所以我尝试手动完成。这是我使用的代码:
\documentclass{amsart}
\newcounter{Week}
\newenvironment{Weeks}
{\begin{list}{{\Large \bf Week \arabic{Week}:}}%
{%
\setlength\labelwidth{.25in}%
\setlength\leftmargin{0in}%
\setlength\itemsep{24 pt}%
\usecounter{Week}}%
}%
{%
\end{list}%
}%
\begin{document}
\begin{Weeks}
\item This is a test
\item 2nd test
\end{Weeks}
\end{document}
它基本上可以满足我的要求,但我想问一下,除了列出带有标签的每一行之外,是否有办法添加换行符,以便您添加的文本出现在标签“周”后的下一行X:“?我知道我可以手动在每个项目上添加换行符——我只是想学习如何在环境定义代码中以另一种方式执行此操作。
答案1
首先,正如@ChristianHupfer 已经在OP的评论中指出的那样,不要使用\bf
。或者LaTeX定义了类似的\bfseries
。其次,该fullpage
包也已弃用。geometry
是设置页面几何的规范方法。
关于解决方案,我建议首先将列表标签的格式抽象为辅助宏——它以后会派上用场:
\newcommand*\marklabel[1]{\bgroup\Large\bfseries#1\egroup}
然后你可以定义一个类似替代方案\item
的宏:
\newcommand*\weekitem[1][]{%
\expandafter\ltxitem
\if\relax\detokenize{#1}\relax\else[#1]\fi
\leavevmode\par\vskip1em\par\noindent
}
(其中\ltxitem
将是默认宏的副本\item
。)
请注意,\weekitem
保留通过可选参数手动指定替代标签的能力。最后,在定义中的列表标题中,\newenvironment
您指定\item
应该让\weekitem
:
\newenvironment{Weeks}{%
\begin{list}{{\marklabel{Week \arabic{Week}}:}}{%
\setlength\labelwidth{.25in}%
\setlength\leftmargin{0in}%
\setlength\itemsep{24pt}%
\usecounter{Week}%
\let\ltxitem\item
\let\item\weekitem}%
}{%
\end{list}%
}
完整代码
\documentclass[11pt]{amsart}
\usepackage{lipsum}
\newcommand*\marklabel[1]{\bgroup\Large\bfseries#1\egroup}
\newcounter{Week}
\newenvironment{Weeks}{%
\begin{list}{{\marklabel{Week \arabic{Week}}:}}{%
\setlength\labelwidth{.25in}%
\setlength\leftmargin{0in}%
\setlength\itemsep{24pt}%
\usecounter{Week}%
\let\ltxitem\item
\let\item\weekitem}%
}{%
\end{list}%
}
\newcommand*\weekitem[1][]{%
\expandafter\ltxitem
\if\relax\detokenize{#1}\relax\else[#1]\fi
\leavevmode\par\vskip1em\par\noindent
}
\begin{document}
\begin{Weeks}
\item This is a test. \lipsum[1]
\item 2nd test
\item[\marklabel{Addendum}:] final test
\end{Weeks}
\end{document}
答案2
您可以更改item
在项目标签后添加换行符的定义
\let\olditem\item
\def\item{\olditem\leavevmode\newline}
完整代码
\documentclass[11pt]{amsart}
\usepackage{geometry}
%\usepackage[parfill]{parskip}
\usepackage{graphicx}
\usepackage{amssymb}
\usepackage[empty,in]{fullpage}
\newcounter{Week}
\newenvironment{Weeks}
{\begin{list}{{\Large \bfseries Week \arabic{Week}:}}%
{%
\let\olditem\item
\def\item{\olditem\leavevmode\newline}
\setlength\labelwidth{.25in}%
\setlength\leftmargin{0in}%
\setlength\itemsep{24 pt}%
\usecounter{Week}}%
}%
{%
\end{list}%
}%
\begin{document}
\begin{Weeks}
\item This is a test
\item 2nd test
\end{Weeks}
\end{document}
答案3
我提出了另一种Weeks
环境变体,基于enumitem
:
\documentclass[11pt]{amsart}
\usepackage{geometry}
\usepackage[parfill]{parskip}
\usepackage{graphicx}
\usepackage{amssymb}
\usepackage[empty,in]{fullpage}
\usepackage{enumitem, calc} %
\newlist{Weeks}{enumerate}{1}
\setlist[Weeks]{label=\raisebox{0pt}[3.5ex][1.5ex]{\makebox[\textwidth][l]{Week \arabic*}}, font =\Large\bfseries, wide = 0pt, itemsep=24pt, leftmargin=\widthof{\Large\textbf{Week}} }
\usepackage{showframe} %
\renewcommand{\ShowFrameLinethickness}{0.3pt}
\begin{document}
Text text text text
\begin{Weeks}
\item This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test.
\item 2nd test
\end{Weeks}
\end{document}
答案4
这是一种使用并enumitem
使用 进行定义的方法。Weeks
\newlist
该计数器被称为Weeksi
(由自动定义enumerate
)
\documentclass[11pt]{amsart}
\usepackage{geometry}
%\usepackage[parfill]{parskip}
\usepackage{graphicx}
\usepackage{amssymb}
\usepackage[empty,in]{fullpage}
\usepackage{enumitem}
\usepackage{xpatch}
\newlist{Weeks}{enumerate}{1}
\AtBeginEnvironment{Weeks}{%\
\xapptocmd{\item}{%
\leavevmode\par%
}{\typeout{Success}}{}
}
\setlist[Weeks,1]{label={\Large \bfseries Week {\arabic*}:},itemsep=24pt, labelwidth=.25in,labelindent=!,listparindent=0em}
\begin{document}
\begin{Weeks}
\item This is a test
\item 2nd test
\item Another Test
\item The \arabic{Weeksi}th Week!
\end{Weeks}
\end{document}