[Tex/LaTex] With knitr and LaTex, how can you arrange the List of Figures alphabetically

knitrr

What code will enable me to reorganize the List of Figures alphabetically (and keep the page number)? My goal is to include that alphabetical listing of figures in the PDF file that is created.

Here is a tiny reproducible example with three figures. Thank you for your assistance.

\documentclass[11pt]{article}  
\begin{document}

\listoffigures

<<source, include = FALSE>>=
library(knitr)
library(ggplot2)
library(datasets)
df <- as.data.frame(Titanic)
@

<<plot1, eval = TRUE, fig.cap = "Plot 1", include = TRUE, warning = FALSE, echo=FALSE>>=
ggplot(df, aes(x=Class, y = Freq)) + geom_point()
@

<<plot2, eval = TRUE, fig.cap = "Another Plot", include = TRUE, warning = FALSE, echo=FALSE>>=
ggplot(df, aes(x=Class, y = Freq)) + geom_point()
@

<<plot3, eval = TRUE, fig.cap = "Third plot", include = TRUE, warning = FALSE, echo=FALSE>>=
ggplot(df, aes(x=Class, y = Freq)) + geom_point()
@

\end{document}

Best Answer

With "reorganize alphabetically" you mean change the numeric counter (1,2,3 ...) by an alphabetic counter (A,B,C...)? Some like this?

mwe

\documentclass[11pt]{article}  
\begin{document}
\renewcommand\thefigure{\Alph{figure}}
\listoffigures

<<plot1, fig.cap = "Plot 1", echo=F>>=
plot(c(1,2,3,4))
@

<<plot2, fig.cap = "Another Plot", echo=F>>=
plot(c(1,2,3,2))
@

<<plot3, fig.cap = "Third plot", echo=F>>=
plot(c(1,2,1,4))
@

\end{document}

But if you mean order by caption contents, then you basically need a indexing package:

mwe

\documentclass[11pt]{article}  
\usepackage{makeidx}
\makeindex
\renewcommand{\indexname}{List of figures}
\begin{document}
\printindex{}

<<plot1, fig.cap = "\\index{Plot 1}Plot 1", echo=F>>=
plot(c(1,2,3,4))
@

<<plot2, fig.cap = "\\index{Another plot}Another Plot", echo=F>>=
plot(c(1,2,3,2))
@

<<plot3, fig.cap = "\\index{Third plot}Third plot", echo=F>>=
plot(c(1,2,1,4))
@


\end{document}

See Layouting the index-page if you want to change the comma delimiter by \dotfill.