2017-10-28 115 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