4

I am working in rmarkdown knitting to pdf. I have set equations (and everything else?) to default to left alignment using

classoption: fleqn

in the YAML metadata at the top of my rmarkdown file.

I was wondering how to specify certain equations to align in the center while still keeping the default alignment of text and equations to left. I have searched this forum and run many examples but the code never seems to work in rmarkdown.

Here is an example rmarkdown document.

---
title: "centering equations"
output:
  pdf_document: default
  html_document: null
  word_document: null
toc: yes
linestretch: 1.3
classoption: fleqn
header-includes: 
 - \setlength{\mathindent}{0pt}
 - \setlength\parindent{0pt}
---

This is some text

This is the equation I would have centered.

\begingroup\large
\begin{align*}
h(t_{ij}) = Pr[T_{i}=j|T_{i}\geq j].
\end{align*}
\endgroup
6
  • 2
    Seems to be a pure rmarkdown question and is therefore off toic here, sorry ...
    – Mensch
    Sep 9, 2017 at 23:53
  • Can you provide an example R Markdown document?
    – Werner
    Sep 9, 2017 at 23:58
  • @Werner I should have supplied that. Have added an example document.
    – llewmills
    Sep 10, 2017 at 0:45
  • use $$ eqn=stuff $$ for your centred equation? Sep 10, 2017 at 1:00
  • Thans @David Carlisle. Unfortunately that still came out left-aligned
    – llewmills
    Sep 10, 2017 at 1:09

1 Answer 1

6

Set the equation inside a box that spans the entire \linewidth:

enter image description here

---
title: "centering equations"
output:
  pdf_document: default
classoption: fleqn
header-includes: 
 - \setlength{\mathindent}{0pt}
 - \setlength{\parindent}{0pt}
---

This is some text

This is the equation I would like left aligned:
\[
  h(t_{ij}) = Pr[ T_i = j| T_i \geq j |
\]

This is the equation I would like centred:
\[
  \makebox[\linewidth]{$h(t_{ij}) = Pr[ T_i = j| T_i \geq j |$}
\]

\noindent
\makebox[\linewidth]{centered text}

This is a set of equations to align centred:
\[
  \makebox[\linewidth]{$\displaystyle
    \begin{aligned}
               f(x) &= ax^2 + bx + c \\
      ax^2 + bx + c &= g(x)
    \end{aligned}
  $}
\]

You must log in to answer this question.

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