3

In the glue package you "can use \\ at the end of a line to prevent adding a newline". In LaTeX \\ is the new line symbol.

I am looking for a better solution than my current one

glue_data(iris,
"\\midrule
\\textbf{{{mean(Petal.Length)}} & 820 &  100\\% \\\\
~other & 902 \\\\"
)

Actual output:

\midrule
\textbf{3.758} & 820 & 100\% \~other & 902 \\

Expected output:

\midrule
\textbf{3.758} & 820 &  100\% \\
~other & 902 \\

My current ugly and error-prone fix:

glue_data(iris,
"\\midrule
\\textbf{{{mean(iris$Petal.Length)}} & 820 &  100\\% \\\\\\
\n~other & 902 \\\\"
)

\midrule
\textbf{3.758} & 820 &  100\% \\
~other & 902 \\
Zoe
  • 27,060
  • 21
  • 118
  • 148
s_baldur
  • 29,441
  • 4
  • 36
  • 69
  • `glue_data` doesn't seem designed to work with LaTeX. Why not use some other way to insert computed values into your LaTeX (e.g. knitr Rnw files) or some other function for macro substitution? – user2554330 Sep 10 '18 at 16:21
  • Good point @user2554330. I like glue's overall syntax and decided I will give a try for this one project. Anyway I already found an improvement to my current solution which is passing the linebreaks in as a variable so glue doesn't interpret them: `lineb <- '\\\\'` and then `... & 100\\% {lineb}` – s_baldur Sep 11 '18 at 07:56
  • You should add your `{lineb}` solution as an answer to your own question. It looks pretty clever. – user2554330 Sep 11 '18 at 10:11
  • a space after `\\` but before the linebreak seems to do the job as well. – s_baldur Sep 18 '18 at 14:33

1 Answers1

0

A better method I found was to add the LaTeX linebreaks in as a variable to prevent them from being interpreted by glue:

lineb <- '\\\\'
glue_data(
  iris,
  "\\midrule
  \\textbf{{{mean(Petal.Length)}} & 820 &  100\\% {lineb}
  ~other & 902 \\\\"
)

Output

\midrule
\textbf{3.758} & 820 &  100\% \\
~other & 902 \\
s_baldur
  • 29,441
  • 4
  • 36
  • 69