2

In Align multiple tables side by side, a user asks about aligning tables, and the (very good) answer provided involves using latex in a chunk with the results returned as is to LaTeX. This is correct for that users question.

I am curious if this can be easily converted into an inline (i.e. not in chunk) solution.

\begin{table}[!htb]
    \begin{minipage}{.5\linewidth}
      \caption{}
      \centering,
       `r t1`,
    \end{minipage}%
    \begin{minipage}{.5\linewidth}
      \centering
        \caption{},
        `r t2`,
    \end{minipage} 
\end{table}

This is almost successful, however the backticks which are required to call the objects from R, end up also being printed to the output. What have I missed? Is it possible to escape the backticks from LaTeX, but not from the R process?

Update: After requests for reproducibility, please see the full markdown of my solution combined with the other answer I linked and referred to as well as a screencap of the output pdf

DaveRGP
  • 1,430
  • 15
  • 34
  • The top answer to the question you posted (and the accepted one) involved putting the tables in a list and calling kable. Did you try that as well? Are t1 and t2 data.frames? – be_green Jan 11 '18 at 16:58
  • In addition, you are probably better off putting it in an R chunk, setting the chunk to `results = 'asis'` and then calling `cat()` around the latex code, as in the question you linked. – be_green Jan 11 '18 at 16:59
  • @be_green, thanks for the advice. I think I fully understand the material in the question linked, and I have an existing solution in my use case implementing `results=as.is` in which the output functions fine. My interest is specifically limited to the implementation I describe, i.e. the inline application. – DaveRGP Jan 11 '18 at 17:08
  • 1
    Oh ok, I misunderstood. Is this just curiosity or is there a separate use-case for this version? – be_green Jan 11 '18 at 17:52
  • Largely curiosity. It could be argued that if available this would be the preferred version due to simplicity, clarity, maintenance etc, but that's not my intent here either :) – DaveRGP Jan 11 '18 at 17:59
  • 1
    @DaveRGP: [This code](https://pastebin.com/NN0Y02Pu) produces [this output](https://i.stack.imgur.com/Fwwce.png), showing that the variables from R are read and printed as expected, even within a bunch of LaTeX code. – Werner Jan 19 '18 at 16:25
  • 1
    I'm voting to close this question as off-topic because the problem can't be reproduced in its current form. – Werner Jan 19 '18 at 23:38
  • @Werner, I've supplied a fuller reprex, and an example of the issue as an output. Apologies for the delay, I've been without a computer. Thanks for the suggestions :) – DaveRGP Jan 29 '18 at 14:29
  • 1
    @DaveRGP: Your new code does not show any backticks. Instead, just like in your code currently visible in your post, you have a number of unnecessary `,` (after `\centering` and/or `\caption{}` and your in-line R statements). *These* are the things you see in your output, not backticks. – Werner Jan 29 '18 at 16:37
  • @Werner, thank you! I (stupidly) hadn't accounted for the original nature of the string I was editing having the commas to separate the items in `c()`, that was not needed when I modified it to inline. That's the answer. Happy to mark that as such if you submit it. – DaveRGP Jan 29 '18 at 16:49

1 Answers1

1

The backticks are interpreted correctly, as is shown in the following example:

---
title: 'A title'
author: 'An author'
date: 'A date'
output: 
  pdf_document
---

```{r include=FALSE}
myvar1 <- 5 * cos(pi / 3)
myvar2 <- 10 * sin(pi / 6)
```

`myvar1` is `r myvar1`; `myvar2` is `r myvar2`.

\begin{table}[!htb]
    \begin{minipage}{.5\linewidth}
      \caption{}
      \centering
       `r myvar1`
    \end{minipage}%
    \begin{minipage}{.5\linewidth}
      \centering
        \caption{}
        `r myvar2`
    \end{minipage} 
\end{table}

In your example the backticks would also not be printed, but the ,s you left as part of your code will be. You may have been mistaken by the ,s looking like backticks.

Werner
  • 14,324
  • 7
  • 55
  • 77