rstudio / bookdown

Authoring Books and Technical Documents with R Markdown
https://pkgs.rstudio.com/bookdown/
GNU General Public License v3.0
3.78k stars 1.27k forks source link

Citation footnote in table caption -> footnote text not rendered at all #760

Open njbart opened 5 years ago

njbart commented 5 years ago

First file, bookdown_table_caption_citation.Rmd, adapted from https://stackoverflow.com/questions/46670422/how-to-insert-a-reference-in-a-table-caption-in-a-bookdown-document-that-works-f

---
title: "Test"
output:
  bookdown::pdf_document2:
    latex_engine: xelatex
    keep_tex: true
csl: chicago-fullnote-bibliography.csl
reference-section-title: References
bibliography: bookdown_table_caption_citation.yaml
---

# A section {#sec:FirstSection}

The dataset in Table \@ref(tab:aTable) contains some data.

# Another section

(ref:aTable-caption) See @doe2019.

```{r, aTable, echo = FALSE}
knitr::kable(
  cars[1:5, ],
  caption  = "(ref:aTable-caption)"
)

Second file, `bookdown_table_caption_citation.yaml`:

references:

latex file (via keep_tex: yes):

\documentclass[]{article}
\usepackage{lmodern}
\usepackage{amssymb,amsmath}
\usepackage{ifxetex,ifluatex}
\usepackage{fixltx2e} % provides \textsubscript
\ifnum 0\ifxetex 1\fi\ifluatex 1\fi=0 % if pdftex
  \usepackage[T1]{fontenc}
  \usepackage[utf8]{inputenc}
\else % if luatex or xelatex
  \ifxetex
    \usepackage{mathspec}
  \else
    \usepackage{fontspec}
  \fi
  \defaultfontfeatures{Ligatures=TeX,Scale=MatchLowercase}
\fi
% use upquote if available, for straight quotes in verbatim environments
\IfFileExists{upquote.sty}{\usepackage{upquote}}{}
% use microtype if available
\IfFileExists{microtype.sty}{%
\usepackage{microtype}
\UseMicrotypeSet[protrusion]{basicmath} % disable protrusion for tt fonts
}{}
\usepackage[margin=1in]{geometry}
\usepackage{hyperref}
\hypersetup{unicode=true,
            pdftitle={Test},
            pdfborder={0 0 0},
            breaklinks=true}
\urlstyle{same}  % don't use monospace font for urls
\usepackage{longtable,booktabs}
\usepackage{graphicx,grffile}
\makeatletter
\def\maxwidth{\ifdim\Gin@nat@width>\linewidth\linewidth\else\Gin@nat@width\fi}
\def\maxheight{\ifdim\Gin@nat@height>\textheight\textheight\else\Gin@nat@height\fi}
\makeatother
% Scale images if necessary, so that they will not overflow the page
% margins by default, and it is still possible to overwrite the defaults
% using explicit options in \includegraphics[width, height, ...]{}
\setkeys{Gin}{width=\maxwidth,height=\maxheight,keepaspectratio}
\IfFileExists{parskip.sty}{%
\usepackage{parskip}
}{% else
\setlength{\parindent}{0pt}
\setlength{\parskip}{6pt plus 2pt minus 1pt}
}
\setlength{\emergencystretch}{3em}  % prevent overfull lines
\providecommand{\tightlist}{%
  \setlength{\itemsep}{0pt}\setlength{\parskip}{0pt}}
\setcounter{secnumdepth}{5}
% Redefines (sub)paragraphs to behave more like sections
\ifx\paragraph\undefined\else
\let\oldparagraph\paragraph
\renewcommand{\paragraph}[1]{\oldparagraph{#1}\mbox{}}
\fi
\ifx\subparagraph\undefined\else
\let\oldsubparagraph\subparagraph
\renewcommand{\subparagraph}[1]{\oldsubparagraph{#1}\mbox{}}
\fi

%%% Use protect on footnotes to avoid problems with footnotes in titles
\let\rmarkdownfootnote\footnote%
\def\footnote{\protect\rmarkdownfootnote}

%%% Change title format to be more compact
\usepackage{titling}

% Create subtitle command for use in maketitle
\providecommand{\subtitle}[1]{
  \posttitle{
    \begin{center}\large#1\end{center}
    }
}

\setlength{\droptitle}{-2em}

  \title{Test}
    \pretitle{\vspace{\droptitle}\centering\huge}
  \posttitle{\par}
    \author{}
    \preauthor{}\postauthor{}
    \date{}
    \predate{}\postdate{}

\begin{document}
\maketitle

{
\setcounter{tocdepth}{2}
\tableofcontents
}
\hypertarget{sec:FirstSection}{%
\section{A section}\label{sec:FirstSection}}

The dataset in Table \ref{tab:aTable} contains some data.

\hypertarget{another-section}{%
\section{Another section}\label{another-section}}

\begin{table}[t]

\caption{\label{tab:aTable}See John Doe.\footnote{\emph{A Book on This and That.} (Place: Publisher, 2019).}}
\centering
\begin{tabular}{r|r}
\hline
speed & dist\\
\hline
4 & 2\\
\hline
4 & 10\\
\hline
7 & 4\\
\hline
7 & 22\\
\hline
8 & 16\\
\hline
\end{tabular}
\end{table}

\hypertarget{bibliography}{%
\section*{References}\label{bibliography}}
\addcontentsline{toc}{section}{References}

\hypertarget{refs}{}
\leavevmode\hypertarget{ref-doe2019}{}%
Doe, John. \emph{A Book on This and That.} Place: Publisher, 2019.

\end{document}

So, in a table apparently handled by rmarkdown/bookdown (rather than pandoc), \footnote{...} is placed inside the table caption, with the result that the footnote text is not rendered at all (well-known latex issue).

Solution: Have bookdown output

\caption[See John Doe.]{\label{tab:aTable}See John Doe.\footnotemark{}}

[EDIT: fixed previous line. Just like pandoc, always use the optional argument of \caption containing the text to be used if a list of tables is generated by latex (i.e. without any label and footnote stuff). ]

instead, and add

\footnotetext{\emph{A Book on This and That.} (Place: Publisher, 2019).}

outside, probably best immediately after the table environment.

BTW, this is exactly how pandoc itself solves this issue (see, e.g., https://github.com/jgm/pandoc/issues/1506 and https://github.com/jgm/pandoc/issues/4683).


By filing an issue to this repo, I promise that

I understand that my issue may be closed if I don't fulfill my promises.

cderv commented 3 years ago

Thanks.

I see the issue but it does not seem easy because I believe the \footnote is created by Pandoc processing when using the csl chicago-fullnote-bibliography.csl Am I right with CSL ?

If this above is true, the issue is that the text reference feature is a hack to allow markdown syntax. But I think we move it after the processing by Pandoc. This means that I don't think Pandoc see the reference in a caption and so may not do the correct replacement. I may be wrong though...

Anyway, I believe this would require patching when post processing in this case.

We would love help on this topic that is specific to some use of CSL allowing footnote with references in caption.

atusy commented 3 years ago

@njbart I have a work around. Try my re-implementation of reference text in Lua filter and let kable output Pandoc's markdown.

  1. download https://raw.githubusercontent.com/atusy/lua-filters/master/lua/bookdown-reference-text.lua
  2. use it with
    bookdown::pdf_document2:
        latex_engine: xelatex
        pandoc_args:
            - "--lua-filter"
            - "bookdown-reference-text.lua"
  3. set kable(format = "pandoc"), i.e.
    knitr::kable(
      cars[1:5, ],
      caption  = "(ref:aTable-caption)",
      format = "pandoc"
    )

image

cderv commented 3 years ago

Thanks a lot @atusy ! Am I right that doing it in a Lua fitler allow to fix

If this above is true, the issue is that the text reference feature is a hack to allow markdown syntax. But I think we move it after the processing by Pandoc. This means that I don't think Pandoc see the reference in a caption and so may not do the correct replacement. I may be wrong though...

I would find it very useful to use a Lua filter for this feature in bookdown. If you are willing to contribute it, it would be quick to have this available. @yihui what do you think about have text reference feature done in Lua ? @atusy does your current filter cover the same scope as bookdown text reference feature ?

Could be limitation with format of table in this case. (Needs to be Markdown)

Also does it solve the issue above by working with CSL chicago-fullnote-bibliography.csl ?

atusy commented 3 years ago

@cderv

I am interested in the contribution, but needs some time. My implementation still lacks an ability to replace text in the RawBlock like LaTeX. I think this requires insertion of Para within RawBlock, which would be not straightforward.

Also does it solve the issue above by working with CSL chicago-fullnote-bibliography.csl ?

Yes

cderv commented 3 years ago

Great! don't worry, there is not hurry at all. I can have a look and help improve if you want.

yihui commented 3 years ago

As I just said at https://github.com/rstudio/bookdown/issues/811#issuecomment-827020479, I'm always looking forward to having my R hacks replaced by the Lua filter!