简体   繁体   English

在 for 循环中包含两个变量之间的空格 r markdown(pdf 输出)

[英]Including space between two variables in a for loop r markdown (pdf output)

I am quite new to r Markdown and I apologize if this is a dumb question.我对 r Markdown 很陌生,如果这是一个愚蠢的问题,我深表歉意。 I am trying to create some stimuli using rMarkdown, and for each trial, I intend to extract two values from two columns( L_Prob & R_Prob ) in a data frame ( D1 ) and to present them side by side.我正在尝试使用 rMarkdown 创建一些刺激,并且对于每个试验,我打算从数据框 ( D1 ) 中的两列 ( L_ProbR_Prob ) 中提取两个值并将它们并排呈现。 I intend to output the r markdown file into pdf.我打算把output r markdown文件改成Z437175BA4191210EE004E1D937494D09 I also intend to include some spaces between the two values;我还打算在两个值之间包含一些空格; however, I am having difficulty.但是,我遇到了困难。 My current code looks like below.我当前的代码如下所示。

{r, echo = F, comment=NA, results='asis'}
D1= data.frame(L_Prob=c("a+b","b+c","c+d"),R_Prob=c("d+e","e+f","f+g"))

for (i in 1:nrow(D1)){

  a = D1%>% select(L_Prob) %>% slice(i) %>% pull
  b = D1%>% select(R_Prob) %>% slice(i) %>% pull
  cat(a,b)
  cat("  \n") 
  cat("Why do you choose this problem?")
  cat("  \n") 
  cat("It looks to be easier to solve")
  cat("  \n") 
  cat("I prefer problems in this number type")
  cat("  \n") 
  cat("No particular reason")
  cat("  \n") 
  cat("Other______________________________")
  cat("\\newpage  ")
}

The output looks like a+b b+c with no space between. output 看起来像a+b b+c之间没有空格。 What I want it to look like is我希望它看起来像

a+b                                 b+c

I have tried cat(a," ",b) which doesn't work.我试过cat(a," ",b)这不起作用。 I am not sure exactly how I can do it and any suggestion would be appreciated.我不确定我该怎么做,任何建议都将不胜感激。 Since I am outputting it into pdf, my understanding is that I cannot left align a and right align b, but please correct me if I am wrong.由于我将它输出到pdf,我的理解是我不能左对齐a和右对齐b,但如果我错了,请纠正我。 Thank you again for your help!再次感谢你的帮助!

Use format() :使用format()

s <- format(c("A", "BB", "CCC"), width=10, justify="centre")
cat(s)

Outputs:输出:

A BB CCC

LaTeX ignores spaces by default. LaTeX 默认忽略空格。 Try \hspace{length} :尝试\hspace{length}

cat(a, "\\hspace{10em}", b)

More on length units.更多关于长度单位。 Play with it until you reach the desired result.玩它,直到达到所需的结果。 Or use \space :或使用\space

cat(a,rep("\\space", 33),b)

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM