Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

In the list of tables and figures, how to set the same right margin for all lines of all the items?

As an example, the following MWE:

\documentclass{article}

\newlength{\largNum}

\newlength{\tocRightMargin}
\setlength{\tocRightMargin}{2cm}

\newlength{\tocLeftMarginSecondLineFigure}
\setlength{\tocLeftMarginSecondLineFigure}{6.5em}

\makeatletter
\renewcommand*\l@figure[2]{%
  \settowidth{\largNum}{\hss #2}
  \ifnum \c@tocdepth >\m@ne%
    \addpenalty{-\@highpenalty}%
    \vskip 1.0em \@plus\p@%
    \setlength\@tempdima{3.4em}%
        \noindent%
    \begingroup
      \pretolerance=10000
      \parindent \z@ \rightskip \tocRightMargin%
      \parfillskip -\tocRightMargin%
     \leavevmode \normalsize%
     \advance\leftskip \tocLeftMarginSecondLineFigure%
      \hskip -\leftskip%
      {\figurename\mbox{\hspace{4pt}}#1}\nobreak%
       \leaders\hbox{$\m@th%
        \mkern \@dotsep mu\hbox{.}\mkern \@dotsep%
        mu$} \hfil\nobreak\hb@xt@%
                    \largNum{\hss #2}\par%
      \penalty\@highpenalty%
    \endgroup
  \fi}
\makeatother

\begin{document}

\listoffigures

\begin{figure}
\caption{Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque justo justo, porta sagittis feugiat eget}
\end{figure}

\end{document}

Produces:

enter image description here

But I would like to have:

enter image description here

share|improve this question
2  
Your example doesn't clearly highlight what you're after since both captions are the same, making the addition of ~~~~ superfluous. I also would not agree with doing this for your entire document, since it would mean that you need to scan and find the longest caption according to which you're align the dot leaders. Am I right? –  Werner Jul 3 '13 at 17:09
    
Both captions are the same, but not in the lof! I would like to avoid the ~~~~~, this was to shown an example of a proper layout in the lof. Also, adding ~~~~~~ may cause aligment problems in centered captions). Thanks. –  NicoGa Jul 3 '13 at 17:11
    
Well exactly. Why do you use ~~~~ in the first place? That's what I meant by saying that it is not entirely clear what you're after. –  Werner Jul 3 '13 at 17:20
    
I am after the proper formatting of the lof. ~~~~ is for the sake of the example as I did not find an other way to properly show desired layout of the lof. Sorry if it wasn't clear! –  NicoGa Jul 3 '13 at 17:36
    
I changed the op after your comments, I hope it is clearer now. Thanks –  NicoGa Jul 3 '13 at 17:47

1 Answer 1

up vote 3 down vote accepted

You want to do

\leaders\hbox{...}\hskip 1\tocRightMargin plus 1fil\nobreak

rather than \hfil. Here's the code, where I made also a few other changes; for example the “Figure n” is typeset in a box with the right width.

\documentclass{article}

\newlength{\largNum}

\newlength{\tocRightMargin} % Marge droite
\setlength{\tocRightMargin}{2cm}

\newlength{\tocLeftMarginSecondLineFigure}
\setlength{\tocLeftMarginSecondLineFigure}{6.5em}

\makeatletter
\renewcommand*\l@figure[2]{%
  \settowidth{\largNum}{#2}%
  \ifnum \c@tocdepth >\m@ne
    \addpenalty{-\@highpenalty}%
    \vskip 1.0em \@plus\p@
    \setlength\@tempdima{3.4em}%
        \noindent
    \begingroup
      \def\numberline##1{\makebox[\tocLeftMarginSecondLineFigure][l]{\figurename\ ##1}}%
      \pretolerance=10000
      \parindent \z@ \rightskip \tocRightMargin
      \parfillskip -\tocRightMargin
     \leavevmode \normalsize
     \advance\leftskip \tocLeftMarginSecondLineFigure
      \hskip -\leftskip
      #1\nobreak
       \leaders\hbox{$\m@th
        \mkern \@dotsep mu\hbox{.}\mkern \@dotsep
        mu$}\hskip 1\tocRightMargin plus 1fil\nobreak
        \hb@xt@\largNum{\hss #2}\par
      \penalty\@highpenalty
    \endgroup
  \fi}
\makeatother

\begin{document}

\listoffigures

\begin{figure}
\caption{Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque justo justo, porta sagittis feugiat eget}
\end{figure}

\end{document}

enter image description here

share|improve this answer
    
Wonderful! That solve my problem! What should I do to thank you (my first post on stackexchange ...) –  NicoGa Jul 3 '13 at 22:52

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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