在rmarkdown中对齐方框内的方程

时间:2017-10-28 22:08:12

标签: latex r-markdown pdflatex

我试图在rmarkdown中的一个方框内得到对齐的方程式。我正在编织到pdf。

框是必要的原因是因为我使用的YAML元数据自动左对齐所有文本,但我希望方程居中。

这是我的rmarkdown代码。正如您在运行时所看到的那样,方程式居中,但第二组=符号未在两条线上对齐。

任何帮助非常感谢。

---
title: "Aligning equations in a box"
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}
---


\setlength{\abovedisplayskip}{-15pt}
\setlength{\belowdisplayskip}{1pt}
\setlength{\abovedisplayshortskip}{1pt}
\setlength{\belowdisplayshortskip}{1pt}

```{r global_options, include=FALSE, echo = FALSE}

knitr::opts_chunk$set(fig.width=12, fig.height=8, fig.path='Figs/', 
                      echo=FALSE, warning=FALSE, message=FALSE, dev = 'pdf')
```

These words are automatically left-aligned by the YAML meta-data above so a box is necessary to centre equations.

### Equation 11.6

\[\large
  \makebox[\linewidth]{$\displaystyle
    \begin{aligned}
              \text{when}\ PT &= 0:logit\ h(t_{j})\ &= [\alpha_{7}D_{7} + \alpha_{8}D_{8} + \cdots + \alpha_{12}D_{12}]\\
              \text{when}\ PT &= 1:logit\ h(t_{j})\ &= [\alpha_{7}D_{7} + \alpha_{8}D_{8} + \cdots + \alpha_{12}D_{12}] + \beta_{1}
    \end{aligned}
  $}
\]

1 个答案:

答案 0 :(得分:1)

不确定我理解,但是否需要在两者之间留出很大的空间?如果没有,您只需删除&

即可
    \[\large
      \makebox[\linewidth]{$\displaystyle
        \begin{aligned}
                  \text{when}\ PT =& 0:logit\ h(t_{j})\ = [\alpha_{7}D_{7} + \alpha_{8}D_{8} + \cdots + \alpha_{12}D_{12}]\\
                  \text{when}\ PT =& 1:logit\ h(t_{j})\ = [\alpha_{7}D_{7} + \alpha_{8}D_{8} + \cdots + \alpha_{12}D_{12}] + \beta_{1}
        \end{aligned}
      $}
    \]

这给出了

enter image description here

如果您想要空格,可以使用\qquad\quad

之类的内容
    \[\large
      \makebox[\linewidth]{$\displaystyle
        \begin{aligned}
                  \text{when}\ PT =& 0:logit\ h(t_{j})\ \qquad= [\alpha_{7}D_{7} + \alpha_{8}D_{8} + \cdots + \alpha_{12}D_{12}]\\
                  \text{when}\ PT =& 1:logit\ h(t_{j})\ \qquad= [\alpha_{7}D_{7} + \alpha_{8}D_{8} + \cdots + \alpha_{12}D_{12}] + \beta_{1}
        \end{aligned}
      $}
    \]

给了我

enter image description here

修改

在评论中回答问题。将块包裹在两个&之间。

    \[\large
      \makebox[\linewidth]{$\displaystyle
        \begin{aligned}
                  \text{whenmoretext}\ PT &= 0:logit\ h(t_{j})\ &  &= [\alpha_{7}D_{7} + \alpha_{8}D_{8} + \cdots + \alpha_{12}D_{12}]\\
                  \text{when}\ PT &= 1:logit\ h(t_{j})\ &  &= [\alpha_{7}D_{7} + \alpha_{8}D_{8} + \cdots + \alpha_{12}D_{12}] + \beta_{1}
        \end{aligned}
      $}
    \]

enter image description here