如何在R Markdown导出PDF时实现文本两端对齐?
Hey there! I totally get the frustration—when working with R Markdown, it's easy to find solutions for text justification in HTML outputs, but PDF feels like a whole different beast. Let's break down how to get that clean, fully justified text for your PDF documents.
Since R Markdown generates PDFs using LaTeX, we need to use LaTeX-based solutions here (the CSS tricks for HTML won't apply). Here are two straightforward methods:
Method 1: Set Justification for the Entire Document
Add these LaTeX commands right after your YAML header (before any markdown content):
\usepackage{ragged2e} \justifying
The ragged2e package gives you better control over text alignment than LaTeX's default commands, and \justifying will apply full justification to every paragraph in your PDF.
Method 2: Justify Specific Paragraphs
If you only want certain sections to be justified instead of the whole document, wrap those paragraphs in LaTeX commands:
\justify Your fully justified text goes here. This will stretch the lines to align perfectly on both the left and right margins, just like you'd see in a printed book or formal report. \endjustify
A quick heads-up: Some R Markdown PDF templates might already have justification enabled by default, so it's worth checking your output first. If not, the above methods should fix it right up.
内容的提问来源于stack exchange,提问作者Adela Iliescu

