leovan / quarto-pseudocode

Quarto Pseudocode Extension
MIT License
38 stars 2 forks source link

请问 quarto-pseudocode 可以在 beamer 上运作吗? #1

Closed ChoCho66 closed 1 year ago

ChoCho66 commented 1 year ago

在 html 以及 pdf 格式上,皆可以成功。 但在 beamer 上会失败。 下面是测试的失败资讯以及测试的小范本。

失败的资讯 ``` Rendering PDF running xelatex - 1 This is XeTeX, Version 3.141592653-2.6-0.999995 (TeX Live 2023) (preloaded format=xelatex) restricted \write18 enabled. entering extended mode updating tlmgr updating existing packages ERROR: compilation failed- error LaTeX Error: Not in outer par mode. See the LaTeX manual or LaTeX Companion for explanation. Type H for immediate help. ... l.211 \end{frame} ```
测试的小范本 ```` --- title: "Quarto Pseudocode Extension Example" filters: - pseudocode format: beamer: include-in-header: text: | \usepackage{xeCJK} include-before-body: text: | \renewcommand{\Return}{\State \textbf{return}~} \newcommand{\Print}{\State \textbf{print}~} \newcommand{\Break}{\State \textbf{break}} \newcommand{\Continue}{\State \textbf{continue}} \newcommand{\True}{\textbf{true}} \newcommand{\False}{\textbf{false}} \renewcommand{\And}{\textbf{and}~} \newcommand{\Or}{\textbf{or}~} \renewcommand{\Not}{\textbf{not}~} \newcommand{\To}{\textbf{to}~} \newcommand{\DownTo}{\textbf{downto}~} pdf-engine: xelatex keep-tex: true --- ## Test text-style Test text-style is shown as @alg-test-text-style. ```pseudocode #| label: alg-test-text-style #| html-indent-size: "1.2em" #| html-comment-delimiter: "//" #| html-line-number: true #| html-line-number-punc: ":" #| html-no-end: false #| pdf-placement: "htb!" #| pdf-line-number: true \begin{algorithm} \caption{Test text-style} \begin{algorithmic} \State asd \end{algorithmic} \end{algorithm} ``` ````

请问需要做什么调整才能在 beamer 格式上运作呢?

leovan commented 1 year ago

晚些我测试看看问题修复下,beamer 也是 PDF 类型,理论上应该 OK。

leovan commented 1 year ago

测试了下,主要原因是因为 beamer 会将每一个片子放在 \begin{frame} \end{frame} 环境中,但这里面不允许有浮动元素,所以利用 pdf-placement 参数指定内容放置的位置即可,例如 #| pdf-placement: "H"

leovan commented 1 year ago

但是不是所有的示例代码都在 beamer 下测试通过了,尤其是带有自定义命令的部分,例如,当使用 \renewcommand{\And}{\textbf{and}~} 时会报错,具体还没来得及看是问什么,先使用 algorithm 包自带的关键字吧,后续有时间会再排查修复下。

ChoCho66 commented 1 year ago

感谢回答! 我刚刚测试了自定义命令的部分。 在 beamer 上出问题感觉是转成 tex 时,他的环境是

\documentclass[
  ignorenonframetext,
]{beamer}

ignorenonframetext 时就没法使用自定义的东西。 我在 overleaf 上将 ignorenonframetext 给 comment 后是可以执行的。 (他会侦测到 \renewcommand{\Not}{\textbf{not}~}有问题,好像应该改成 \newcommand{\Not}{\textbf{not}~})。 只是我不知道怎么在 quarto 上设定让它不要使用 ignorenonframetext

以下是我使用的 qmd 和其转成的 tex 档 以及 将ignorenonframetext comment 在 overleaf 上的截图。

qmd ```` --- title: "Quarto Pseudocode Extension Example" filters: - pseudocode # classoption: "" format: beamer: # include-in-header: # text: | # \usepackage{xeCJK} include-before-body: text: | \renewcommand{\Return}{\State \textbf{return}~} \newcommand{\Print}{\State \textbf{print}~} \newcommand{\Break}{\State \textbf{break}} \newcommand{\Continue}{\State \textbf{continue}} \newcommand{\True}{\textbf{true}} \newcommand{\False}{\textbf{false}} \renewcommand{\And}{\textbf{and}~} \newcommand{\Or}{\textbf{or}~} \renewcommand{\Not}{\textbf{not}~} \newcommand{\To}{\textbf{to}~} \newcommand{\DownTo}{\textbf{downto}~} # pdf-engine: xelatex keep-tex: true --- ## Test ```pseudocode #| pdf-line-number: true #| pdf-placement: "H" \begin{algorithm} \caption{Test} \begin{algorithmic} \State \textbf{Bools:} \And \Or \Not \State \True \State \False \end{algorithmic} \end{algorithm} ``` ````
tex ```tex % Options for packages loaded elsewhere \PassOptionsToPackage{unicode}{hyperref} \PassOptionsToPackage{hyphens}{url} % \documentclass[ ignorenonframetext, ]{beamer} \usepackage{pgfpages} \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} \usepackage{iftex} \ifPDFTeX \usepackage[T1]{fontenc} \usepackage[utf8]{inputenc} \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 \usepackage{lmodern} \ifPDFTeX\else % xetex/luatex font selection \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} \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} \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} \makeatother \makeatletter \makeatother \makeatletter \makeatother \makeatletter \@ifpackageloaded{caption}{}{\usepackage{caption}} \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}} \makeatother \makeatletter \@ifpackageloaded{caption}{}{\usepackage{caption}} \@ifpackageloaded{subcaption}{}{\usepackage{subcaption}} \makeatother \makeatletter \@ifpackageloaded{tcolorbox}{}{\usepackage[skins,breakable]{tcolorbox}} \makeatother \makeatletter \@ifundefined{shadecolor}{\definecolor{shadecolor}{rgb}{.97, .97, .97}} \makeatother \makeatletter \makeatother \makeatletter \ifdefined\Shaded\renewenvironment{Shaded}{\begin{tcolorbox}[frame hidden, borderline west={3pt}{0pt}{shadecolor}, breakable, interior hidden, enhanced, boxrule=0pt, sharp corners]}{\end{tcolorbox}}\fi \makeatother \makeatletter \makeatother \makeatletter \@ifpackageloaded{algorithm}{}{\usepackage{algorithm}} \makeatother \makeatletter \@ifpackageloaded{algpseudocode}{}{\usepackage{algpseudocode}} \makeatother \ifLuaTeX \usepackage{selnolig} % disable illegal ligatures \fi \IfFileExists{bookmark.sty}{\usepackage{bookmark}}{\usepackage{hyperref}} \IfFileExists{xurl.sty}{\usepackage{xurl}}{} % add URL line breaks if available \urlstyle{same} % disable monospaced font for URLs \hypersetup{ pdftitle={Quarto Pseudocode Extension Example}, hidelinks, pdfcreator={LaTeX via pandoc}} \title{Quarto Pseudocode Extension Example} \author{} \date{} \begin{document} \frame{\titlepage} \renewcommand{\Return}{\State \textbf{return}~} \newcommand{\Print}{\State \textbf{print}~} \newcommand{\Break}{\State \textbf{break}} \newcommand{\Continue}{\State \textbf{continue}} \newcommand{\True}{\textbf{true}} \newcommand{\False}{\textbf{false}} \renewcommand{\And}{\textbf{and}~} \newcommand{\Or}{\textbf{or}~} \renewcommand{\Not}{\textbf{not}~} \newcommand{\To}{\textbf{to}~} \newcommand{\DownTo}{\textbf{downto}~} \floatname{algorithm}{Algorithm} \begin{frame}{Test} \protect\hypertarget{test}{} \begin{algorithm}[H] \caption{Test} \begin{algorithmic}[1] \State \textbf{Bools:} \And \Or \Not \State \True \State \False \end{algorithmic} \end{algorithm} \end{frame} \end{document} ```
截图 ![image](https://github.com/leovan/quarto-pseudocode/assets/72805700/0679dff8-fbe6-485a-b4c8-49d40ce80777)
leovan commented 1 year ago

参考 https://github.com/jgm/pandoc/issues/3295,将重新定义的命令放在每页上就可以正常使用了,但需要在所有用到的页面都放上。如果将其放在 include-in-header 中,也会有一些小问题,后面系统引入的一些包可能会把你定义的命令再次重定义(例如:\Not)。

---
title: "Quarto Pseudocode Extension Example"
filters:
  - pseudocode
format:
  beamer:
    include-in-header:
      text: |
        \usepackage{xeCJK}
    pdf-engine: xelatex
    keep-tex: true
---

## Test atoms

Test atoms is shown as @alg-test-atoms.

\renewcommand{\Return}{\State \textbf{return}~}
\newcommand{\Print}{\State \textbf{print}~}
\newcommand{\Break}{\State \textbf{break}}
\newcommand{\Continue}{\State \textbf{continue}}
\newcommand{\True}{\textbf{true}}
\newcommand{\False}{\textbf{false}}
\renewcommand{\And}{\textbf{and}~}
\newcommand{\Or}{\textbf{or}~}
\renewcommand{\Not}{\textbf{not}~}
\newcommand{\To}{\textbf{to}~}
\newcommand{\DownTo}{\textbf{downto}~}

```pseudocode
#| label: alg-test-atoms
#| html-line-number: false
#| html-no-end: true
#| pdf-line-number: false
#| pdf-placement: "H"

\begin{algorithm}
\caption{Test atoms}
\begin{algorithmic}
\State \textbf{Specials:} \{ \} \$ \& \# \% \_
\State \textbf{Bools:} \And \Or \Not \True \ \False
\State \textbf{Carriage return:} first line \\ second line
\State \textbf{Text-symbols:} \textbackslash
\State \textbf{Quote-symbols:} `single quotes', ``double quotes''
\State \textbf{Math:} $(\mathcal{C}_m)$, $i \gets i + 1$, $E=mc^2$, \( x^n + y^n = z^n \), $\$$, \(\$\)
\end{algorithmic}
\end{algorithm}