3

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

egreg
  • 1,121,712
NicoGa
  • 53
  • 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 03 '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 03 '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 03 '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 03 '13 at 17:36
  • I changed the op after your comments, I hope it is clearer now. Thanks – NicoGa Jul 03 '13 at 17:47
  • Could you include a picture of what you're really after in addition to the current (unwanted) output. Even if it means that you have to do it in some other format (MS Word, or Paint). If you don't have enough reputation, include the image URLs. Someone with edit privileges will re-embed the images. Why? It's still not clear to me what you're after exactly. It's not you, it's me... – Werner Jul 03 '13 at 18:22
  • OK I changed again the OP according to your advice. I hope it is clear enough now. Thanks. – NicoGa Jul 03 '13 at 21:52
  • @NicoGa We don't use to add [SOLVED] to the title; the answer and the comments suffice. – egreg Jul 03 '13 at 23:16

1 Answers1

2

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

egreg
  • 1,121,712