3

The follwoing result is easily accieved in Word:

example

How is this possible in Latex?

Description:

  1. First line without indentation and fixed tabulator at, say, 2 cm
  2. Starting from the second line, indentation of 2 cm

That is, if the first column is short enough , then there is a tabulator (or a jump to the second column) otherwise the line extends in the second column and is separated by a comma+space.

Can a glossary handle such a thing? At the end this should be combined with my post here.

  • That can be easily done with a description list and the enumitem package. – Ulrike Fischer May 18 at 8:30
  • @UlrikeFischer I didn't look recently in the documentation of enumitem, but would it really handle the conditional appending of the comma? – frougon May 18 at 13:38
  • @frougon you can always measure the label and do something depending on the length. – Ulrike Fischer May 18 at 14:02
  • @UlrikeFicher Hmm, right. But that's not competely out-of-the-box available behavior, then. – frougon May 18 at 14:09
3

You can achieve the result with enumitem:

\documentclass{article}
\usepackage{enumitem}

\usepackage{lipsum}

\newlength{\commalabelwd}
\newcommand{\commalabel}[2]{%
  \settowidth\commalabelwd{\normalfont\itshape#2,\hspace{\labelsep}}%
  \normalfont\itshape#2\ifdim#1<\commalabelwd,\fi\hfill
}

\begin{document}

\begin{description}[
  labelwidth=\dimexpr2cm-\labelsep,
  leftmargin=2cm,
  before={\renewcommand\makelabel[1]{\commalabel{2cm}{##1}}}
]
\item[Short] \lipsum[1][1-2]

\item[Longer] \lipsum[1][1-2]

\item[Longerr] \lipsum[1][1-2]

\item[Longerrr] \lipsum[1][1-2]

\item[Longerrrr] \lipsum[1][1-2]

\item[Longerrrrr] \lipsum[1][1-2]

\item[Longerrrrrr] \lipsum[1][1-2]

\item[Longerrrrrrr] \lipsum[1][1-2]

\item[Longerrrrrrrrrrrrr] \lipsum[1][1-2]
\end{description}

\end{document}

enter image description here

  • This doesn't do the conditional comma addition mentioned in the question... – frougon May 18 at 15:38
  • @frougon Right, I oversaw it. Fixed. – egreg May 18 at 16:04
  • very interesting solution and with less verbosity as the other; the use of \mathscr in the item label has the consequence that the content of the item is treated as maths environment; example \item[$\mathscr{P}[a,b]$ ] set of all partitions, then "set of all..." is in maths mode – PeptideChain May 18 at 17:46
  • 2
    @PeptideChain You need to protect the brackets in the optional argument: \item[{$\mathscr{P}[a,b]$}], otherwise the first ] would be taken as the delimiter; this is a common problem with optional arguments. – egreg May 18 at 17:50
  • thank you, I understand now! with {....} it works. I already had this problem months ago...and I changed the label... – PeptideChain May 18 at 18:31
2

I had to solve exactly the same problem not long ago. The approach is similar to that of frougon:

\documentclass{article}

\usepackage[papersize={5.5in,8.5in}]{geometry}
\usepackage{lipsum}

%% Change the optional argument to suit -- 1in in this case
\newcommand{\hangit}[2][1in]{%
    \hangindent#1
    \setbox0=\hbox{\itshape#2\space}
    \noindent
    \ifdim\wd0>\myhang
        \textit{#2,}\space
    \else
        \hbox to #1{\box0\hfill}%
    \fi
    \ignorespaces
}

%% #1 default indent; #2 text in indent; #3 main text; #4 text after dot leaders
\newcommand{\hangitpar}[4][1in]{%
    \hangindent#1
    \setbox0=\hbox{\itshape#2\space}
    \noindent
    \ifdim\wd0>\myhang
        \textit{#2,}\space
    \else
        \hbox to #1{\box0\hfill}%
    \fi
    #3\mindotfill#4%
}

\newcommand{\mindotfill}{%
  \nolinebreak
  {\def\hfill{\hskip 1cm plus 1fill\relax}%
   \dotfill
  }%
}

%% Same, but uses an argument delimited with a +
%% Requires \newlength{\myhang} 
%% Could also be given explicitely in the \def
\newlength{\myhang}
\setlength{\myhang}{1in}
\def\hhangit#1+{%
    \hangindent\myhang
    \setbox0=\hbox{\itshape#1\space}
    \noindent
    \ifdim\wd0>\myhang
        \textit{#1,}\space
    \else
        \hbox to \myhang{\box0\hfill}%
    \fi
    \ignorespaces
}

\begin{document}

\lipsum[5][1-4]

\hangitpar{A longer test of sorts}{\lipsum[7][4-7]}{123}

\hangit{First one}\lipsum[3][4-7]

%% Use of optional argument:
\hangit[0.75in]{Second is a bit longer}\lipsum[8][4-7]

%% Use of + delimited argument:
\hhangit And this is the next longer+ \lipsum[9][4-7]\mindotfill 123

\lipsum[6][1-4]

\end{document}

Text in hanging indent plus dot leaders

Update

The OP's request for dot leaders finally sank in. I have altered the code and the example accordingly. The code for \mindotfill is taken from frougon's answer in Adjusting dotfill for custom table of notations

1

You can do like this:

\documentclass{article}
\usepackage{xcolor}
\usepackage{lipsum}

\newlength{\myColWidth}
\setlength{\myColWidth}{4em}
\newlength{\myColSep}
\setlength{\myColSep}{1em}

% A comma is appended if, and only if \widthof{#1} >= \myColWidth-\myColSep.
% The second column always starts at \myColWidth from the left margin. In
% other words, \myColSep is internal to the first column.
\newenvironment{conceptEntry}[1]{%
  \smallskip\par\noindent
  \hangindent=\myColWidth\hangafter=1\relax
  \setbox0=\hbox{#1}%
  \ifdim\wd0<\dimexpr \myColWidth-\myColSep \relax
    \hbox to \myColWidth{\box0\hfil}%
  \else
    \setbox0=\hbox{\box0, }%
    \ifdim\wd0<\myColWidth
      \hbox to \myColWidth{\box0\hfil}%
    \else
      \box0\relax
    \fi
  \fi
  \ignorespaces
}{%
  \par
  \ignorespacesafterend
}

\newcommand*{\showCommaThreshold}{%
  \par\noindent
  \begingroup
    \color{blue}%
    \rule{\dimexpr \myColWidth-\myColSep\relax}{1pt}%
  \endgroup
}

\begin{document}
The entry text (\emph{Lorem ipsum} here) starts precisely at
\verb|\myColWidth| from the left margin. In blue, we show the tunable
threshold for the entry key width, after which a comma is appended (its value
is \verb|\myColWidth-\myColSep|):
\showCommaThreshold

\begin{conceptEntry}{Short}
  \lipsum[1][1-4]
\end{conceptEntry}

\showCommaThreshold
\begin{conceptEntry}{Longe.}
  \lipsum[1][1-4]
\end{conceptEntry}

\showCommaThreshold
\begin{conceptEntry}{Longer}
  \lipsum[1][1-4]
\end{conceptEntry}

\begin{conceptEntry}{Even longer}
  \lipsum[1][1-4]
\end{conceptEntry}
\end{document}

Screenshot

For the dotted line you seem to want to add afterwards, I would try to get inspiration from \@dottedtocline (a LaTeX macro used to typeset “dotted lines” table of contents entries—see source2e.pdf). The essential idea is to reserve space to the right of the paragraph using \rightskip, except that this space can be occupied by the last line of the paragraph (which ends with dots and the page or whatever number). This is what the following setup:

\rightskip \@tocrmarg \parfillskip -\rightskip

ensures in the definition of \@dottedtocline, where \@tocrmarg is the

right margin indentation for all but last line of multiple-line entries

(quote from source2e.pdf).

  • the solution works perfectly, nice idea to have two parameters to control the behavior. I'm looking into the problem of the filling with dots, because this is the way I have to follow, and I have to adapt your answer to the other Q. I'm looking the code in the link. If I can't solve it , I'll open a new Q. Thank you – PeptideChain May 18 at 18:35
  • Starting from egreg's solution (which I confess leads to a nicer input syntax), you can obtain this. The horizontal rule at the top has length \textwidth, and I used this morning's \mindotfill. This is 11 relatively short lines away from egreg's solution. :-) Input syntax for a term is like \item[Short] \lipsum[1][1-2]\glosspagenum{3}. – frougon May 18 at 19:22
  • 1) The 11 lines don't count \mindotfill's definition (6 lines). 2) For those who pass by, “this morning's \mindotfill” referred to this answer. – frougon May 18 at 19:34
  • where is the \glosspagenum defined? how do you have generated the picture? I think, that I have missed a step – PeptideChain May 18 at 19:53
  • 1
    It's all there now. :-) – frougon May 19 at 12:49

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.