Make the lines of a description item line up
If i did understand you correctly, the labeling
environment provided by the KOMA-script package does the job you want:
\documentclass[12pt,a4paper]{article}
\usepackage{scrextend}
\usepackage{blindtext}
\setkomafont{labelinglabel}{\bfseries}
\usepackage{showframe}
\begin{document}
\begin{labeling}{A long long label}%Giving the longest label for alignment
\item [Label]
Here is a long item that requires a line break. The second
line of this item won't line up with the first word. I can
achieve this manually\\
by forcing a line break and setting an apropriate value in
\textbackslash hspace*, but I want this to be automised.
Please help me.
\item [A long long label]
a manual linebreak, though not
recommended \\
\blindtext
\end{labeling}
\end{document}
Here package scrextend
provides all you need, switching to a koma class (scrartcl
) could be an advantage. The labeling
environment takes the longest label as the argument, needed for the right alignment. This argument can also be shorter, just play a bit.
As an alternative to a KOMA class or to scrextend
, you can use the enumitem
package, setting the key labelwidth
to the width of your label.
I got this solution by combining (a) Bernard's answer to Simple chronology using the description environment and (b) Bernard's answer to How to set leftmargin of description to width of a particular label in enumitem?. You can find the same solution in Gonzalo Medina's answer to Description list with aligned descriptions.
\documentclass[12pt,a4paper]{article}
\usepackage{enumitem}
\begin{document}
\begin{description}[%
labelwidth=3cm,%
leftmargin=!%
]
\item [Label]Here is a long item that requires a line break. The second line of this item
won't line up with the first word. I no longer need to achieve this manually by forcing a
line break and setting an appropriate value in \textbackslash hspace*.
Here's a second paragraph of the same item.
\item [A longer label]Here is a second item. It has a longer label, necessitating a larger labelwidth.
\end{description}
\end{document}
Here's the output:
You can also calculate the width of the widest label by adding the calc
package and using its \widthof
command:
\documentclass[12pt,a4paper]{article}
\usepackage{enumitem}
\usepackage{calc}% Supports \widthof command
\begin{document}
\begin{description}[%
labelwidth=\widthof{\bfseries A longer label},%
leftmargin=!%
]
\item [Label]Here is a long item that requires a line break. The second line of this item
won't line up with the first word. I no longer need to achieve this manually by forcing a
line break and setting an appropriate value in \textbackslash hspace*.
Here's a second paragraph of the same item.
\item [A longer label]Here is a second item. It has a longer label, necessitating a larger labelwidth.
\end{description}
\end{document}
This gives essentially the same output. Note in \widthof{\bfseries A longer label}
, the necessity of specifying \bfseries
: The label is typeset as bold. You need to make sure that the text whose width you measure with \widthof
is also typeset as bold.
You can go a step further in automation by having the environment automatically determine which label is widest. See Gonzalo Medina's answer to Automatically set description list labelwidth
based on widest label?.