# With knitr and LaTex, how can you arrange the List of Figures alphabetically?

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}


## migrated from stackoverflow.comNov 22 '15 at 21:37

This question came from our site for professional and enthusiast programmers.

• Technically this has nothing to do with knitr. You want LaTeX's \listoffigures to sort the content. – Werner Nov 22 '15 at 18:54
• OK, I guess the question is in programming purgatory, in limbo between R and LaTeX. Does anyone have a suggestion? – lawyeR Nov 24 '15 at 13:21

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

\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:

\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.

• Closer, and perhaps the \renewcommand\thefigure{\Alph{figure}} is the direction I need to follow. But I want the output to show Another Plot first, then Plot 1, then Third plot -- in other words, in alphabetical order. I do not care about the A, B, C in the first column. Can LaTeX do that alphabetizing? – lawyeR Jan 1 '16 at 14:15
• @lawyeR See the update. It help now? – Fran Jan 1 '16 at 16:01
• Most of my 150+ images have a \caption command after an \includegraphic command, not a fig.cap in the first line of the chunk. How can I put the index command into the \caption command (I hope this makes sense, as I am a LaTeX newbie). – lawyeR Jan 2 '16 at 0:36
• @lawyeR \caption{\index{xxx}xxx} – Fran Jan 2 '16 at 2:09

In the end, using the open source R programming language, this is the code that alphabetized the list of figures.

lofigs <- readLines("~/[name of your file.lof")

regmatch <- str_match_all(lofigs, "\\\\ignorespaces (.+)\\\\relax \\}\\}\\{(.+?)\\}")

regmatch2 <- lapply(regmatch,
function(x) {
#                       if (str_detect(x[1, 2], "(####|\\*)")) {
#                         str_match(x[1, 2], "(.+) (?:####|\\*)(.+)")[[1]]
if (str_detect(x[1, 2], "(?:####|\\*)")) {
str_match_all(x[1, 2], "(.+) (?:####|\\*)(.+)")[[1]]
} else {
c(NA, x[1, 2], NA)
}
})

regmatch <- do.call(rbind, regmatch)
regmatch2 <- do.call(rbind, regmatch2)

image.pg.df <- cbind(regmatch2[, 2:3], regmatch[, 3])
colnames(image.pg.df) <- c("ImagePlot", "Figure", "Page")


I ended up sorting and cleaning it in Excel and then using this code in a chunk in my .Rnw file (using knitr package)

lofigs.arranged$Page <- as.integer(lofigs.arranged$Page)
print(xtable(x = lofigs.arranged), include.rownames = FALSE, tabular.environment = "longtable", floating = FALSE)