我正在try 让Quarto在呈现为both个PDF和HTML时正确对齐一些文本.但我只能让它做其中的一个,不能两个都做.下面是一个最小的例子,带有PDF和HTML输出的源代码和屏幕截图.您可以看到源代码成功地将LaTeX \begin{flushright}用于PDF输出,并成功地将::: {style="text-align: right"}用于HTML输出,但两者不能很好地结合在一起.还有别的办法吗?

---
title: "How to right justify both html and pdf"
---

Here is some intitial text in usual left-justified format.

::: {style="text-align: right"}
Attempt 1. This text should be right justified.
:::

\begin{flushright}
Attempt 2. This text should be right justified.
\end{flushright}

::: {style="text-align: right"}
\begin{flushright}
Attempt 3. This text should be right justified.
\end{flushright}
:::

\begin{flushright}
::: {style="text-align: right"}
Attempt 4. This text should be right justified.
:::
\end{flushright}

Here is some *more* text in usual left-justified format.

HTML output. Notice only Attempt 1 appears (correctly), but the other attempts don't appear at all: enter image description here


PDF output. Notice Attempt 1 does not work: enter image description here

推荐答案

当PanDoc1开始将标记文档转换为Quarto生成的html格式时,它会取消LaTeX环境(在本例中为flushright).这就是为什么您在html输出中看不到LaTeX环境中的文本.

现在,为了获得LaTeX和html的正确对齐文本,一个巧妙的技巧是使用PandocDiv(即:::).我建议您阅读this whole section on Pandoc Divs syntax and how it works from Rmarkdown Cookbook页了解详细信息.

简单地说,下面的div将转换为

::: {.flushright data-latex=""}
Attempt 1. This text should be right justified.
:::

将latex 输出为,

\begin{flushright}
Attempt 1. This text should be right justified.
\end{flushright}

并将其输出为,

<div class="flushright">
<p>Attempt 1. This text should be right justified.</p>
</div>

您还可以为flushright类定义任意数量的css属性.

quarto_file.qmd

---
title: "How to right justify both html and pdf"
format: 
  pdf: default
  html: default
---

```{=html}
<style>
.flushright {
   text-align: right;
}
</style>
```

Here is some intitial text in usual left-justified format.

::: {.flushright data-latex=""}
Attempt 1. This text should be right justified.
:::

Here is some *more* text in usual left-justified format.

100

output rendered in pdf format


100

output rendered in html format


1 Pandoc is used by Quarto for document conversion, just like R-markdown.

Html相关问答推荐

如何防止下拉菜单内的Bootstrap列表行包装?

如何将值从Google Sheets侧边栏中的表单中的输入传递到Google Sheets单元格中使用Textile()

输入表单在奇怪的地方舍入的标签

使用无限数量的元素创建特定的CSS网格

响应网格,响应调整大小以适应父div,保留父div S自己的响应高度

Style=背景图像URL引用变成&;Quot;

在Vue 3中使用v-Bind将计算(computed)属性传递给css url()

HTMLTag属性内的svelte+TS类型声明

使用CSS和Slick Carcass使图像适合屏幕

是否可以只使用css而不使用像&;nbsp;这样的HTML符号来实现断字而不换行空格?

如何显示自定义按钮以将产品添加到WooCommerce购物车?

列表项文本在占据剩余空间之前在标记下换行

如何在Go模板中传入途中创建的 map

perl hdb 调试器:浏览器以错误的编码显示 UTF-8 源代码

如何在黑暗模式切换中添加图标

如何修剪 flex: 1 内的垂直文本?

HTML 如何根据屏幕尺寸调整/裁剪视频背景?

如何并排放置
的定义列表,但如果
变长,
会向下移动?

鼠标悬停时切换 colored颜色 的双色链接

无法使用 HTML 中的 Bootstrap 自上而下居中