9

I'm trying to implement a command to be used in place of the itemize/enumerate environments. The idea is that I want to have a command (\detail), which can be used to construct an itemized list without being contained in an environment. The problem I'm having is that when a line is longer than the page width and consequently wraps to the next line, I don't seem to have any control over the indentation of the wrapped text. I've tried the hanging package and \hangindent, but haven't had much luck.

I've included a minimum example below and the output that I get when compiled. Ideally, I would like the wrapped text to be indented such that the first word on the second line aligns with the first word of the first line.

\documentclass{article}
\newcommand{\detail}[1]{-- {#1}}

\begin{document}
    \noindent\textbf{This is a header}\\
    \detail{This is a detail.}\\
    \detail{This is a detail that has too many words in it and consequently runs onto the next line of the page, and I want it to have a hanging indent.}
\end{document}

enter image description here

4

A simplification of the already answered:

\documentclass{article}
\def\D{\par\noindent\makebox[1em][l]{-- }\hangindent1em}
\begin{document}
\subsubsection*{This is a header}
\D This is a detail
\D This is a detail that has too many words in it and consequently runs onto The next line of the page, and I want it to have a hanging indent.
\end{document}

MWE

  • Ok, so this aligns perfectly whereas the previous answer did not. Can you explain to me why this works better? – Kevin Apr 20 '15 at 18:13
  • @Kevin I think that is only a optical effect because the shape of the "T". In the answer of Harish Kumar, change the "next" of the last line by "Text". Both "T" are now perfectly aligned. Try only a text with \noindent T\par \noindent n\par and you will see that n seem also here misaligned with respect the "T", but both characters are just in the margin! – Fran Apr 20 '15 at 18:45
  • It's not an illusion. My actual document has two capital "M"s, one directly beneath the other. With Harish Kumar's answer, the "M"s do not align, but with yours they do. – Kevin Apr 20 '15 at 18:57
  • @Kevin I needed a zoom at 400% to note that, but you are right. I do not know why the result is not exactly the same. – Fran Apr 20 '15 at 19:53
  • Why is the wrapped line starting with a capital T? – Ibn Saeed Apr 21 '15 at 3:36
8
\documentclass{article}
\newcommand{\detail}[1]{\par\noindent\hangindent=\mylen\hangafter1-- #1}
\newlength{\mylen}
\settowidth{\mylen}{-- }

\begin{document}
    \noindent\textbf{This is a header}
    \detail{This is a detail.}
    \detail{This is a detail that has too many words in it and consequently runs onto the next line of the page, and I want it to have a hanging indent.}
\end{document}

enter image description here

  • Awesome! That's almost perfect. For some reason, the alignment is just a hair off, which becomes really apparent when a hanging line starts with the same letter: i.snag.gy/Zb3N5.jpg – Kevin Apr 20 '15 at 2:21
  • @Kevin My eyes are pretty bad. Try with \settowidth{\mylen}{--\space} – user11232 Apr 20 '15 at 2:26
  • Nope, that has the same effect. I tried with other bullet icons in case it was the en dash that was messing it up, but they still don't come out exactly aligned. – Kevin Apr 20 '15 at 2:32
  • @HarishKumar -- the space after the dash will be included in the justification process, so unless all the spaces after the justification are absolutely equal to the "natural" width, that initial space will cause the first letter of the text to be out of line with the hanging indent. put it in a box. (the \settowidth works, because that will use the "natural" width of the space.) – barbara beeton Apr 21 '15 at 16:35
  • @barbarabeeton I doubted that. Thanks. :-) – user11232 Apr 21 '15 at 23:19
2
\documentclass{article}
\newcommand{\detail}[1]{\par\noindent\hangindent=\mylen\hangafter1--\,\,#1}
\newlength{\mylen}
\settowidth{\mylen}{--\,\,}

\begin{document}
    \noindent\textbf{This is a header}
    \detail{This is a detail.}
    \detail{This is a detail that has too many words in it and consequently runs onto the next line of the page, and I want it to have a hanging indent.}
\end{document}

Wanted to put this as comment to Harish Kumar's answer but I have not enough rep for that. Anyway, since space is rubber it's better to use a fixed length. Since \, seems a bit narrow I used it twice, you can adjust to taste.

  • There's a reason for the rep threshold for comments, but it's not so that you can post answers that should have been comments. ;-) A mod will convert this to a comment for you. You're really close to the comment privilege (50 rep)! One solid question or answer will put you over the edge. Cheers, and thanks for spending time at TeX.SX! :-) – Paul Gessler Apr 20 '15 at 3:55
  • Using this method, I still can't seem to get the correct amount of white space on the hanging indent. It's still just a hair off. – Kevin Apr 20 '15 at 17:06

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.