我有以下代码:
\documentclass{article}
\usepackage{color}
\usepackage{lipsum}
\newcommand{\itemColor}[1]{\item[\llap{\textcolor{blue}{#1}\hspace*{.75mm}}]}
\newcommand{\listConfig}{
\setlength{\topsep}{-10pt} % amount of extra vertical space at top of list
\setlength{\partopsep}{\baselineskip} % extra length at top if environment is preceded by a blank line (it should be a rubber length)
\setlength{\parsep}{\baselineskip} % amount of vertical space between paragraphs within an item
\setlength{\itemsep}{-\baselineskip} % amount of extra vertical space between items
\setlength{\leftmargin}{2cm} % horizontal distance between the left margins of the environment and the list; must be nonnegative
\setlength{\rightmargin}{0cm} % horizontal distance betwen the right margins of the enviroment and the list; must be nonnegative
\setlength{\itemindent}{0pt} % indentation of first line of an item; can be negative
\setlength{\labelsep}{2mm} % separation between end of the box containing the label and the text of the first line of an item
}
\newenvironment{descr}{%
\vspace{.1in}
\begin{list}{}{%
\listConfig
\setlength{\itemsep}{0pt}
}
}{\end{list}\vspace{.1in}}
\begin{document}
This is a descriptive environment:
\begin{descr}
\itemColor{title 1}\lipsum[1]
\itemColor{long long long long long long long title}\lipsum[1]
\itemColor{test}\lipsum[1]
\end{descr}
\end{document}
产生的结果如下:
如您所见,标题过长看起来不太好看。但是,我没有想到在命令中手动或自动换行的方法itemColor
。有什么办法可以解决这个问题吗?
答案1
您可能想要定制\makelabel
。
\documentclass{article}
\usepackage{color}
\usepackage{lipsum}
%% \newcommand{\itemColor}[1]{\item[\llap{\textcolor{blue}{#1}\hspace*{.75mm}}]}
\newcommand{\listConfig}{%
% amount of extra vertical space at top of list:
\setlength{\topsep}{-10pt}%
% extra length at top if environment is preceded by a blank line (it
% should be a rubber length):
\setlength{\partopsep}{\baselineskip}%
% amount of vertical space between paragraphs within an item:
\setlength{\parsep}{\baselineskip}%
% amount of extra vertical space between items
\setlength{\itemsep}{-\baselineskip}%
% horizontal distance between the left margins of the environment
% and the list; must be nonnegative:
\setlength{\leftmargin}{2cm}%
% horizontal distance betwen the right margins of the enviroment and
% the list; must be nonnegative:
\setlength{\rightmargin}{0cm}%
% indentation of first line of an item; can be negative:
\setlength{\itemindent}{0pt}%
% separation between end of the box containing the label and the
% text of the first line of an item:
\setlength{\labelsep}{2mm}%
}
\newenvironment{descr}{%
\vspace{.1in}
\begin{list}{}{%
\listConfig
% doesn't all of this belong to \listConfig?
\setlength{\itemsep}{0pt}%
\setlength{\labelwidth}{2cm}%
\renewcommand\makelabel[1]{{\color{blue}\smash{\parbox[t]{\dimexpr\labelwidth-.75mm\relax}
{\raggedleft\strut##1\strut}}\hfil}}%
}%
}{\end{list}\vspace{.1in}}
\begin{document}
This is a descriptive environment:
\begin{descr}
\item[title 1]\lipsum[1]
\item[long long long long long long long title]\lipsum[1]
\item[test]\lipsum[1]
\end{descr}
\end{document}
答案2
这enumitem
包可以做到这一点。此包允许您使用对自定义列表环境(例如description
)<key>=<value>
。以下键+值几乎可以满足您的要求:(来自文档)
style=multiline
:标签放置在一个 parbox 中,其宽度为 leftmargin,必要时可以多行显示。与 相同style=standard,align=parleft,labelwidth=!
。
唯一的问题是align=parleft
导致标签左对齐而不是右对齐。不幸的是enumitem
无法识别\parright
,但我们可以通过向键\raggedleft
中添加 来解决这个问题\font=
。
我认为这段代码可以实现你想要的功能:
\documentclass{article}
\usepackage{lipsum} %% <- for \lipsum
\usepackage{xcolor}
\usepackage{enumitem}
\begin{document}
%% Note: this \leavevmode is necessary, see the addendum:
\begin{description}[align=parleft,labelwidth=!,
font=\raggedleft\normalfont\leavevmode\color{blue},
leftmargin=2cm]
\item[title 1] \lipsum[66] %% <- \lipsum[66] is short
\item[long long long long long long long title] \lipsum[66]
\item[test] \lipsum[66]
\end{description}
\end{document}
您可以添加其他键,例如topsep=<...>
或itemsep=<...>
等,以设置您调整的其他参数。请参阅enumitem
文档这里了解更多信息。
替代方案:您也可以parright
通过复制 的定义parleft
并替换\raggedright
来定义自己\raggedleft
,如下所示:
\SetLabelAlign{parright}{\strut\smash{\parbox[t]\labelwidth{\raggedleft#1}}}
如果您随后使用parright
而不是,则可以从键中parleft
省略。\raggedleft
font=
如果您打算多次使用此样式,您也可以description
通过在前言中添加以下行来一次性更改所有环境。当然,您可以添加我之前提到的其他键。
\setlist[description]{align=parleft,labelwidth=!,font=\raggedleft\normalfont\leavevmode\color{blue},leftmargin=2cm}
如果您想要更频繁地使用这种风格,但又不想重新定义默认描述环境,那么您可以使用以下命令创建自己的环境:
\newlist{mydescr}{description}{1} %% last argument is the maximum depth.
\setlist[mydescr]{align=parleft,labelwidth=!,font=\raggedleft\normalfont\leavevmode\color{blue},leftmargin=2cm}
附录:
我\leavevmode
的font=\raggedleft\normalfont\leavevmode\color{blue}
是必要的,因为似乎\color
不能在“出于技术原因”的开头使用\parbox
。我不知道为什么会这样,所以我刚刚问了一个问题关于它,已经已回答。
答案3
如果您想在长标题中换行,我会使用表格而不是列表,但如果您在如此狭窄的列中换行,结果可能不会很漂亮。我想我会选择一个正常的描述环境,其中长标题将在文本正文的第一行继续。
\documentclass{article}
\usepackage[table]{xcolor}
\usepackage{enumitem}
\usepackage{lipsum}
\usepackage{array}
\setlist{
font=\normalfont\bfseries\color{blue}
}
\begin{document}
\begin{description}
\item[title 1] \lipsum[1]
\item[long long long long long long long title] \lipsum[1]
\item[test] \lipsum[1]
\end{description}
\begin{tabular}{>{\color{blue}\bfseries\raggedleft}p{.15\textwidth}>{\color{black}}p{.8\textwidth}}
title 1 & \lipsum[1]\\
long long long long long long long title & \lipsum[1]\\
test & \lipsum[1]
\end{tabular}
\end{document}