2017-10-28 114 views
1

我想在rmarkdown的框中获得对齐方程。我正在编织pdf。在rmarkdown中对齐方框内的方程

框是必要的原因是因为我使用的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

不知道我明白了,但是有必要有一个大的空间吗?如果没有,你可以只取出&

\[\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

+0

谢谢@shituoshan。这很好,但不是'&'应该锚定'='并且使它们跨行对齐? – llewmills

+0

@llewmills是的,这就是你在'PT ='后面做的事情,并且在行尾用'\\'分开它。这里的工作原理是:\ text {when} \ PT =&0:logit'和\ text {when} \ PT = 1:logit'长度相同。 – shitoushan

+1

所以@shitoushan如果在其中一个文本之前有额外文本(例如\ text {whenthereismoretext}而不是\ text {when}时,我将如何获得第二组等号? – llewmills