Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Beamer and theorems #3650

Closed
4 tasks done
samcarter opened this issue Dec 13, 2022 · 6 comments · Fixed by #7535
Closed
4 tasks done

Beamer and theorems #3650

samcarter opened this issue Dec 13, 2022 · 6 comments · Fixed by #7535
Assignees
Labels
bug Something isn't working crossref
Milestone

Comments

@samcarter
Copy link

samcarter commented Dec 13, 2022

Bug description

With quarto 1.3.49 on macOS 11.6, a simple test document with a theorem (https://stackoverflow.com/q/74780867/2777074)

---
title: "Untitled"
format: beamer
editor: visual
---

## Quarto

Quarto enables you to weave together content and executable code into a finished presentation. To learn more about Quarto presentations see <https://quarto.org/docs/presentations/>.

::: {#thm-test}
## Just a quick test
:::

will lead to a non-compilable document, because you try to define the already defined theorem environment. It would be better to check if an environment already exists before you try to define it.

Some other comments about the generated .tex file:

% Options for packages loaded elsewhere
\PassOptionsToPackage{unicode}{hyperref}

with current hyperref, the unicode option is hardly necessary, it is now the default

\PassOptionsToPackage{hyphens}{url}
%
\documentclass[
  ignorenonframetext,

this will cause problems further down

]{beamer}
\usepackage{pgfpages}

unnecessary. Even if note pages are shown, beamer will load it automatically

\setbeamertemplate{caption}[numbered]
\setbeamertemplate{caption label separator}{: }
\setbeamercolor{caption name}{fg=normal text.fg}
\beamertemplatenavigationsymbolsempty
% Prevent slide breaks in the middle of a paragraph
\widowpenalties 1 10000
\raggedbottom
\setbeamertemplate{part page}{
  \centering
  \begin{beamercolorbox}[sep=16pt,center]{part title}
    \usebeamerfont{part title}\insertpart\par
  \end{beamercolorbox}
}
\setbeamertemplate{section page}{
  \centering
  \begin{beamercolorbox}[sep=12pt,center]{part title}
    \usebeamerfont{section title}\insertsection\par
  \end{beamercolorbox}
}
\setbeamertemplate{subsection page}{
  \centering
  \begin{beamercolorbox}[sep=8pt,center]{part title}
    \usebeamerfont{subsection title}\insertsubsection\par
  \end{beamercolorbox}
}
\AtBeginPart{
  \frame{\partpage}
}
\AtBeginSection{
  \ifbibliography
  \else
    \frame{\sectionpage}
  \fi
}
\AtBeginSubsection{
  \frame{\subsectionpage}
}

\usepackage{amsmath,amssymb}

unnecessary, beamer loads them by default

\usepackage{lmodern}
\usepackage{iftex}
\ifPDFTeX
  \usepackage[T1]{fontenc}
  \usepackage[utf8]{inputenc}

unnecessary, that's the default for several years now

  \usepackage{textcomp} % provide euro and other symbols
\else % if luatex or xetex
  \usepackage{unicode-math}
  \defaultfontfeatures{Scale=MatchLowercase}
  \defaultfontfeatures[\rmfamily]{Ligatures=TeX,Scale=1}
\fi
% Use upquote if available, for straight quotes in verbatim environments
\IfFileExists{upquote.sty}{\usepackage{upquote}}{}
\IfFileExists{microtype.sty}{% use microtype if available
  \usepackage[]{microtype}
  \UseMicrotypeSet[protrusion]{basicmath} % disable protrusion for tt fonts
}{}
\makeatletter
\@ifundefined{KOMAClassName}{% if non-KOMA class
  \IfFileExists{parskip.sty}{%
    \usepackage{parskip}
  }{% else
    \setlength{\parindent}{0pt}
    \setlength{\parskip}{6pt plus 2pt minus 1pt}}
}{% if KOMA class
  \KOMAoptions{parskip=half}}
\makeatother
\usepackage{xcolor}

unnecessary, beamer loads it by default

\newif\ifbibliography
\setlength{\emergencystretch}{3em} % prevent overfull lines
\setcounter{secnumdepth}{-\maxdimen} % remove section numbering


\providecommand{\tightlist}{%
  \setlength{\itemsep}{0pt}\setlength{\parskip}{0pt}}\usepackage{longtable,booktabs,array}
\usepackage{calc} % for calculating minipage widths
\usepackage{caption}
% Make caption package work with longtable
\makeatletter
\def\fnum@table{\tablename~\thetable}
\makeatother
\usepackage{graphicx}

unnecessary, beamer loads them by default

\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}
% Set default figure placement to htbp
\makeatletter
\def\fps@figure{htbp}

makes no sense in a class without floating mechanism

\makeatother

\makeatletter
\makeatother
\makeatletter
\makeatother
\makeatletter
\@ifpackageloaded{caption}{}{\usepackage{caption}}

Why? A couple of lines above you force loading the caption package

\AtBeginDocument{%
\ifdefined\contentsname
  \renewcommand*\contentsname{Table of contents}
\else
  \newcommand\contentsname{Table of contents}
\fi
\ifdefined\listfigurename
  \renewcommand*\listfigurename{List of Figures}
\else
  \newcommand\listfigurename{List of Figures}
\fi
\ifdefined\listtablename
  \renewcommand*\listtablename{List of Tables}
\else
  \newcommand\listtablename{List of Tables}
\fi
\ifdefined\figurename
  \renewcommand*\figurename{Figure}
\else
  \newcommand\figurename{Figure}
\fi
\ifdefined\tablename
  \renewcommand*\tablename{Table}
\else
  \newcommand\tablename{Table}
\fi
}
\@ifpackageloaded{float}{}{\usepackage{float}}
\floatstyle{ruled}
\@ifundefined{c@chapter}{\newfloat{codelisting}{h}{lop}}{\newfloat{codelisting}{h}{lop}[chapter]}
\floatname{codelisting}{Listing}
\newcommand*\listoflistings{\listof{codelisting}{List of Listings}}
\usepackage{amsthm}

unnecessary, beamer loads them by default

\theoremstyle{plain}
\newtheorem{theorem}{Theorem}[section]
\theoremstyle{remark}
\renewcommand*{\proofname}{Proof}
\newtheorem*{remark}{Remark}
\newtheorem*{solution}{Solution}
\makeatother
\makeatletter
\@ifpackageloaded{caption}{}{\usepackage{caption}}

you've already loaded it two times, now three and the next line will also load it

\@ifpackageloaded{subcaption}{}{\usepackage{subcaption}}
\makeatother
\makeatletter
\@ifpackageloaded{tcolorbox}{}{\usepackage[many]{tcolorbox}}
\makeatother
\makeatletter
\@ifundefined{shadecolor}{\definecolor{shadecolor}{rgb}{.97, .97, .97}}
\makeatother
\makeatletter
\makeatother
\ifLuaTeX
  \usepackage{selnolig}  % disable illegal ligatures
\fi
\IfFileExists{bookmark.sty}{\usepackage{bookmark}}{\usepackage{hyperref}}

hyperref is loaded by default in beamer

\IfFileExists{xurl.sty}{\usepackage{xurl}}{} % add URL line breaks if available
\urlstyle{same} % disable monospaced font for URLs
\hypersetup{
  pdftitle={Untitled},
  hidelinks,
  pdfcreator={LaTeX via pandoc}}

\title{Untitled}
\author{}
\date{}

\begin{document}
\frame{\titlepage}
\ifdefined\Shaded\renewenvironment{Shaded}{\begin{tcolorbox}[borderline west={3pt}{0pt}{shadecolor}, enhanced, frame hidden, breakable, interior hidden, boxrule=0pt, sharp corners]}{\end{tcolorbox}}\fi

you are loading the beamer class with the ignorenonframetext option. This redefinition will never be seen


\begin{frame}{Quarto}
\protect\hypertarget{quarto}{}
Quarto enables you to weave together content and executable code into a
finished presentation. To learn more about Quarto presentations see
\url{https://quarto.org/docs/presentations/}.

\leavevmode\vadjust pre{\hypertarget{thm-test}{}}%
\begin{theorem}[Just a quick test]\label{thm-test}

~

\end{theorem}
\end{frame}



\end{document}

(It seems that xelatex is the default engine. Is this really such a good idea? It neither has the speed advantage of pdflatex, nor the goodies of lualatex but comes with a truck load of problems, like missing text on beamer note pages, incorrect uncovering of overlays...)

Checklist

  • Please include a minimal, fully reproducible example in a single .qmd file? Please provide the whole file rather than the snippet you believe is causing the issue.
  • Please format your issue so it is easier for us to read the bug report.
  • Please document the RStudio IDE version you're running (if applicable), by providing the value displayed in the "About RStudio" main menu dialog?
  • Please document the operating system you're running. If on Linux, please provide the specific distribution.
@samcarter samcarter added the bug Something isn't working label Dec 13, 2022
@cscheid
Copy link
Collaborator

cscheid commented Dec 13, 2022

It seems that xelatex is the default engine. Is this really such a good idea?

xelatex offers unicode support that pdflatex doesn't; you can always switch the pdf engine yourself, but we do care deeply about internationalization and pdflatex doesn't offer a sufficiently good path forward. So we're sticking with xelatex.

@samcarter
Copy link
Author

samcarter commented Dec 13, 2022

@cscheid You get the same unicode support with lualatex without all the disadvantages of xelatex

A bit of background on xelatex vs. lualatex https://www.reddit.com/r/LaTeX/comments/rrrfbz/comment/hqkwgnb/?utm_source=share&utm_medium=web2x&context=3 (the answer by @ zauguin)

@cscheid cscheid added this to the v1.3 milestone Feb 27, 2023
@dragonstyle dragonstyle modified the milestones: v1.3, v1.4 Feb 27, 2023
@dragonstyle
Copy link
Collaborator

Note- for the original issue regarding theorems, you can workaround the error by disabling theorems in Beamer using:

classoption: ['notheorems']

Also note to self- consider revisiting xelatex vs lualatex default pdf engine.

@cderv
Copy link
Collaborator

cderv commented Feb 28, 2023

There are a lot of feedback in the first post. I haven't process all but just wanted to add a few things:

@sam Regarding anything on the .tex produced, we are building on top of Pandoc's default latex template. So if anything is to modified in how beamer is supported in the default template, this would be to be done upstream. I know some improvement has been made for beamer already and available in Pandoc 3 that we should have now made available in Quarto 1.3 pre-release.

Regarding the theorems now and the conflict, there is a workaround which not to use the cross refer theorem feature in your beamer slide. Using it will trigger the insertion of definition in the doc, which conflicts as you said. However, you could use latex-environment extension to produce use the environment defined in beamer.

Example

---
title: "Example"
format: 
  beamer:
    theme: Xiaoshan # https://www.ctan.org/pkg/pgfornament-han 
filters:
   - latex-environment
environments: [fact, problem, theorem, lemma]
---    

## some blocks

::: {.theorem options="Metropolis"}
A theorem
:::

::: {.lemma options="Metropolis"}
A lemma
:::

## Some other blocks

::: {.fact options="A fact"}
Some interesting stuff
:::

::: {.problem}
A problem
:::

Drawback is that cross referencing using Quarto syntax is not possible.

It would be better to check if an environment already exists before you try to define it.

@dragonstyle FWIW this is the approach we have taken in bookdown at the time when we needed to solve the same issue. (rstudio/bookdown#1143 and PR rstudio/bookdown#1145)
In bookdown, we don't add anymore any definition of theorems and proofs env for the user as some beamer theme like metropolis defines their own, and among them some not available by default in bookdown.

However, when using bookdown::beamer_presentation2 format, we still support the cross reference of bookdown supported format.

It is the same issue in Quarto for now I think - no easy way to use fact environment defined by beamer theme for example, and still reference theorems and others Quarto supported envs

---
title: "Example"
format: 
  beamer:
    theme: Xiaoshan # https://www.ctan.org/pkg/pgfornament-han 
filters:
   - latex-environment
environments: [fact, problem]
---    

## some blocks

::: {#thm-1}

## Metropolis

A theorem
:::

::: {#lem-1}

## Metropolis

A lemma
:::

See @thm-1 and @lem-1.

## Some other blocks

::: {.fact options="A fact"}
Some interesting stuff
:::

::: {.problem}
A problem
:::
compilation failed- error
LaTeX Error: Command \theorem already defined.
```


Here is an example document that works on R Markdown 
<details>
<summary>Rmd</summary>

````markdown
---
title: "My maths"
output: 
  bookdown::beamer_presentation2: 
    toc: no
    latex_engine: xelatex
    theme: Xiaoshan # https://www.ctan.org/pkg/pgfornament-han 
---

## some blocks from book

::: {.theorem #thm-1 name="Metropolis"}
A theorem
:::

::: {.lemma  #lem-1 name="Metropolis"}
A lemma
:::

See \@ref(thm:thm-1) and \@ref(lem:lem-1).

## Some other blocks

::: {.fact latex="[A fact]"}
Some interesting stuff
:::

::: {.problem latex=true}
A problem
:::

In bookdown, we allow the user to opt-out auto addition of definition to provide it manually - if so, cross ref still works.

options(bookdown.theorem.enabled = FALSE)

Sharing as this can be useful to think about if we want to / can provide same flexibility

@devmotion
Copy link

I ran into the same issue with the JuliaCon proceedings class (https://github.com/JuliaCon/JuliaConSubmission.jl/blob/d574a4f172a1074abbf23f93719f9811bfeaa7c3/paper/juliacon.cls): It already defines \proof etc., so when I include a theorem in my qmd file compilation will fail with Command \proof already defined since quarto automatically injects \usepackage{amsthm} ... in the preamble. IMO the class could/should be improved but for the submission I have to work with the given class file, so that's not an option.

I worked around this issue with latex-environment + environments: [theorem], it seems there is no other workaround currently? I think an option to disable these automatic injections in https://github.com/quarto-dev/quarto-cli/blob/56da834f07f5fdfab1e432f11aa3be6b26f4fd2a/src/resources/filters/crossref/meta.lua would be very useful. And, as discussed above, I guess in the case of theorems, remarks, etc., it would be good to check first if the environments are already defined and load amsthm and inject the definitions only if they are not defined yet.

@cderv cderv added the needs-discussion Issues that require a team-wide discussion before proceeding further label Mar 6, 2023
@cderv cderv self-assigned this Mar 6, 2023
@devmotion
Copy link

I was wondering, if one possible approach could be to only set an option (e.g., theorem-header) from within Quarto if it is not set by the user and then conditionally on this setting add LaTeX code in the LaTeX template (`$(if theorem-header)$$theorem-header$$end$). I think maybe in that way it would be easier for templates to not only opt out but also to modify some of these injections, or only include some of the injections (e.g., for theorems but not for tcolorbox).

@cscheid cscheid self-assigned this Mar 29, 2023
@allenmanning allenmanning removed the needs-discussion Issues that require a team-wide discussion before proceeding further label Apr 6, 2023
@cscheid cscheid mentioned this issue May 23, 2023
61 tasks
@dragonstyle dragonstyle removed their assignment Aug 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working crossref
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants