5

I have the following code:

\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}

Which produce this:

enter image description here

As you can see, it do not look nice with a long title. However, I did not think a way to break lines, manually or automatically, in my itemColor command. Any idea how I could solve this problem?

0
8
+50

You probably want to customize \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}

enter image description here

7

The enumitem package can do this. This package lets you customise list environments (such as description) using <key>=<value> pairs. The following key+value almost does what you want: (from the documentation)

style=multiline: the label is placed in a parbox whose width is leftmargin, with several lines if necessary. Same as style=standard,align=parleft,labelwidth=!.

The only problem is that align=parleft causes the labels to be left aligned instead of right aligned. Unfortunately enumitem does not recognise \parright, but we can remedy this by also adding \raggedleft to the \font= key.

I think this code thus does what you want:

\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}

enter image description here

You can add other keys, like topsep=<...> or itemsep=<...> etc., to set the other parameters you've tweaked. See the enumitem documentation here for more information.


Alternative: you could also define parright yourself by copying the definition of parleft and replacing \raggedright by \raggedleft, like this:

\SetLabelAlign{parright}{\strut\smash{\parbox[t]\labelwidth{\raggedleft#1}}}

If you then use parright instead of parleft you can leave out the \raggedleft from the font= key.


If you plan to use this style more than once you can also change all description environments in one go by adding the following line to your preamble. You can of course add the other keys I've mentioned before.

\setlist[description]{align=parleft,labelwidth=!,font=\raggedleft\normalfont\leavevmode\color{blue},leftmargin=2cm}

If you want to use this style more often but don't want to redefine the default description environment, you can create your own environment using:

\newlist{mydescr}{description}{1} %% last argument is the maximum depth.
\setlist[mydescr]{align=parleft,labelwidth=!,font=\raggedleft\normalfont\leavevmode\color{blue},leftmargin=2cm}

Addendum:

The \leavevmode in my font=\raggedleft\normalfont\leavevmode\color{blue} was necessary because it appears that \color can't be used at the start of a \parbox "for technical reasons". I had no idea why this is, so I've just asked a question about it, which has already been answered.

3

If you want line breaks in your long long title, I'd use a tabular instead of a list, but the result will probably not be very pretty if you break lines in such a narrow columns. I think I would go for a normal description environment where long title will continue in the first line of the text body.

\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}

enter image description here

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.