-1

I'd like to do the following in LaTeX (it's being fed to pandoc for conversion) I use the mainfont and mainfontoptions in pandoc. I have a piece of text on top that I'd like centered, and set to the fonts specified in the command line. The rest of the text I'd like to just be the default.

If I put in something like:

\centering Text

It centers fine, but also centers the rest of the document. It also doesn't use the font I want to use.

If I do

# Text

It uses the font (and makes it bold) but doesn't center. Ideally, I'd like to figure out some way to do a one-line \center and #.

2
  • 8
    # used this way is definitely not LaTeX. Maybe you wanted to use {\centering\Huge Text\par}, or \begin{center}\Huge Text\end{center}.
    – frougon
    Commented Apr 5, 2020 at 22:08
  • (question bumped by Community) OP briefly mentioned pandoc. Question could have pandoc added however
    – user202729
    Commented Dec 27, 2021 at 12:57

3 Answers 3

2

Not sure why the SE bot bumped this question up after 3 years, but it does need some explanation:

Many LaTeX commands apply an effect, which continues indefinitely. Among these are \centering, \large, and similar commands.

In order to confine the effect, it is necessary to use a group. If the extent of the effect is very lengthy (much typing), the best approach is to bound the group using \begingroup and \endgroup because they are easier to find, when you edit the text. But if the effect does not involve much typing, it is easy to use braces { } to delimit the group.

Example: {\centering This text is centered.\par}

Example: {\large This text is large} but this is not.

Note that \centering requires \par within the group. Also, you cannot use another \par mid-way, but you can break lines using \\ if desired.

Some commands can span multiple paragraphs within the group. Other commands must confine their effect to within a single paragraph. There are many possible situations, depending on the command (and possibly depending on which package defines the command. Experiment.

1

This old question try to center a # Text that become bold with LaTeX code, so it is clear that is trying to center a section title in Rmarkdown. To avoid giving an outdated answer to an outdated question, let us assume that the problem arises with the newer Quarto (only subtly different to Rmarkdown, but enough to have to point it):

---
format: pdf
---

# Introduction

Lore ipsum dolor sit amet [Introduction].

Although Quarto allow include raw LaTeX code, including \centering in the section title is wrong way. Let see:

Option A:

\section{\centering Introduction}\label{introducion}
  1. This could work in some cases, but concretely not with the default document class of Quarto (scrartcl) where \centering is just ignored.

  2. This prevent to Quarto label automatically the section, so the link [Introduction]will be not exported to \hyperref[introduction]{Introduction} but to {[}Introduction{]}. i.e., plain text with brackets.

Option B:

{\centering\Large\sffamily\bfseries Introduction \par}

This mimic a but a centered section title, but is just plain text, useless for ToC and hyperlinks, with different vertical spacing, etc., so it is even a worse option.

Option C (the good one)

Redefine \section in a custom class, or in a custom pandoc template, or in the YAML header. For the default class of Quarto this is rather simple, so place it in the YAML header it does not bother too much:

---
format: pdf
include-in-header: 
- text: \addtokomafont{section}{\centering}
- text: \usepackage{lipsum}
---

# Introduction

Lore ipsum dolor sit amet  [Introduction]. \lipsum[1][2-5]
  

mwe


2
  • 1
    IMHO \let\raggedsection\centering would be better than \addtokomafont{section}{\centering}, because \centering is not a font attribute. If needed, redefinition of \sectionlinesformat could also be an option.
    – cabohah
    Commented Jun 17 at 7:52
  • @cabohah yes, the semantic purpose of ` \addtokomafont` is for font attributes only, but really there are not a limitation at this respect, So, use another kind of commands here it's a dirty trick, that if it doesn't work no one can claim thais is a bug, but when it works, I can live with the inappropriate use. Anyway, thanks to point to more polite alternatives. +1
    – Fran
    Commented Jun 17 at 20:00
-1

It appears that I can do:

\Huge\centerline{Text}\large

and get what I want

3
  • 2
    I'm afraid this is not very good. 1) \large will affect whatever comes after it. 2) \centerline is from plain TeX, not LaTeX (sometimes, this can be acceptable, though). 3) The scope of \Huge is not limited here (though \large will override it). See my suggestions above—I hope they work in your system.
    – frougon
    Commented Apr 5, 2020 at 22:11
  • THe above works, but prevents the line from line-wrapping, whereas using \begin... /end wraps it up. I actually want the rest of the text large, anyway, so no big deal. Thx
    – Rory Toma
    Commented Apr 6, 2020 at 2:28
  • 4
    It would be better to use \centerline{\Huge text}{\large other text}.
    – user30471
    Commented Apr 6, 2020 at 2:41

Not the answer you're looking for? Browse other questions tagged .