R Markdown有序列表层级切换后HTML输出无空行问题求助
Hey there! I’ve dealt with this exact quirk in R Markdown before—nothing’s more annoying than PDF and HTML outputs behaving differently for the same list structure. Let’s break down how to fix it.
First, the root of the issue is that the HTML renderer (powered by Pandoc) is pickier about list hierarchy formatting than the LaTeX renderer used for PDFs. Your original code doesn’t indent the sublist items, which causes Pandoc to interpret them as top-level list items instead of children of the second ordered list item. Even if PDFs render them correctly, HTML needs clearer structure.
Here are a few reliable fixes:
1. Properly Indent Sublist Items + Add a Blank Line
Make sure your sublist items are indented by 4 spaces (or one tab) to mark them as children of the previous ordered list item. Then add a blank line between the last sublist item and the next top-level ordered list item. This tells Pandoc to close the sublist and start a new top-level list block:
1. This is a thing 2. This is another thing + This is some subpoint thing + This, too, is something 3. This thing should have an empty line above it. It does, when compiled as pdf, it doesn't in html.
2. Use an Empty HTML Comment as a Separator
If the indentation and blank line alone don’t work, insert an empty HTML comment between the sublist and the next top-level item. This acts as a clear "break" for Pandoc’s renderer:
1. This is a thing 2. This is another thing + This is some subpoint thing + This, too, is something <!-- --> 3. This thing should have an empty line above it. It does, when compiled as pdf, it doesn't in html.
3. Switch to GitHub Flavored Markdown (GFM)
GFM handles list formatting more intuitively for HTML outputs. Add markdown: gfm to your document’s YAML header to use this renderer:
--- title: "Your Document Title" output: html_document: markdown: gfm ---
The reason your earlier fixes (two spaces, \newline) didn’t work is that those are for line breaks within a single list item, not for separating different list blocks. We need to signal to Pandoc that we’re closing one list block and starting another, which blank lines (or the comment trick) do effectively.
内容的提问来源于stack exchange,提问作者Rincewind

