[Tex/LaTex] List of tables and List of figures right margin

indentationmarginstable of contents

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

Best Answer

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