You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Quarto Beamer输出中创建自定义div类?尝试方法未生效

在Quarto Beamer中创建自定义div类/幻灯片样式

一、局部内容的自定义div类(对应:::语法)

你之前用::: {.customshrink}没生效,核心原因是Quarto默认不会把Markdown的div直接映射到LaTeX环境,得显式配置映射关系。

解决步骤:

  1. 在文档的YAML头部添加divs-to-environments选项,指定div类对应的LaTeX环境名:
---
format:
  beamer:
    include-in-header: preamble.tex
    theme: metropolis
    divs-to-environments:
      customshrink: customshrink  # 左侧是Markdown div类名,右侧是你定义的LaTeX环境名
---
  1. 用::: {.customshrink}包裹需要应用样式的局部内容:
::: {.customshrink}
This is a test slide with some content. It has a few points:
- Point one
- Point two
...(其余内容)
:::

这样Quarto就会自动把这个div转换成你在preamble.tex里定义的customshrink LaTeX环境,应用字体缩小、间距调整的样式。

二、给整个幻灯片应用自定义样式(对应slide标题加{.customshrink})

如果想让整张幻灯片都应用样式,仅在标题加{.customshrink}没用,因为Quarto不会自动把slide类关联到LaTeX环境,有两种简便方法:

方法1:手动调用LaTeX环境

在幻灯片内容的最开头加\begin{customshrink},结尾加\end{customshrink}:

## Test slide

\begin{customshrink}
This is a test slide with some content...(所有幻灯片内容)
\end{customshrink}

方法2:自动关联slide类与环境

如果不想手动写LaTeX命令,可以在preamble.tex里添加以下代码,让带customshrink类的幻灯片自动应用环境:

\newenvironment{customshrink}
  {
    \scriptsize%
    \setlength{\parskip}{0pt}%
    \setlength{\itemsep}{1pt}%
    \setlength{\topsep}{0pt}%
  }
  {
    % nothing needed here
  }

% 新增:自动给带customshrink类的幻灯片应用环境
\makeatletter
\def\beamer@checkframetitle{\@ifnextchar\bgroup{\beamer@inlineframetitle}{\beamer@frametitle}}
\def\beamer@inlineframetitle#1{%
  \ifx\relax#1\relax\else
    \def\beamer@frametitle{#1}%
    \ifnum\pdfmatch{customshrink}{#1}>0
      \begin{customshrink}
      \gdef\customshrink@active{1}
    \fi
  \fi
}
\AtEndEnvironment{frame}{%
  \ifdefined\customshrink@active
    \end{customshrink}
    \let\customshrink@active\undefined
  \fi
}
\makeatother

之后只要在幻灯片标题后加{.customshrink},整张幻灯片就会自动应用样式。

关键提示

  • divs-to-environments是Quarto实现类似HTML自定义CSS类效果的核心配置,专门用来关联Markdown div和LaTeX环境。
  • 幻灯片级别的样式需要借助LaTeX代码关联,因为Quarto Beamer的幻灯片本质是LaTeX的frame环境。

内容的提问来源于stack exchange,提问作者Maël

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.12 19:20:09