vincentarelbundock / tinytable

Simple and Customizable Tables in `R`
https://vincentarelbundock.github.io/tinytable
GNU General Public License v3.0
211 stars 18 forks source link

Linebreaks in Latex outputting \textbackslash{}, when using `format_tt(escape = "latex")` #266

Closed writezhe closed 5 months ago

writezhe commented 5 months ago

Hi there, thanks for all the work on this package and modelsummary! I've been shifting my work over to these two packages. And my apologies in advance if I've missed something simple here.

I'm trying to add a few linebreaks to regression and summary tables. I've seen in the documentation (link) and in recent Github issues (link) the suggestion to use {\\\\} to ensure the latex output receives the correct \\ in the .tex file.

However, in my testing, this doesn't work correctly if format_tt(escape = "latex") is used. Instead, the \\\\ gets converted to \textbackslash{} twice.

Is this intended? I find this causes a conflict when I want to use a special character in my text, e.g. the % symbol. I can only do one or the other.

I can see why this would be ambiguous — e.g. does the user want two backslashes or do they want a linebreak? I looked through StackExchange and the Github issues, if there's an alternative that I missed, I appreciate it! Thanks in advance!

Replication example:

packageVersion("tinytable") # [1] ‘0.3.0’
packageVersion("modelsummary")  # [1] ‘2.1.0’
library(modelsummary)
library(tinytable)

model_list <- list(
  `My Model {\\\\} (% change)` = mod <- lm(mpg ~ 1, data = mtcars)
)

# correctly outputs \\ for the latex output
modelsummary(model_list,
             output = "tinytable",
             notes = c( "does this work?",
                        "oh {\\\\} yeah")) %>% 
  format_tt(escape = FALSE) %>%
  print("latex")

# causes latex output to get incorrectly commented out due to
#   % character not being escaped
modelsummary(model_list,
             output = "tinytable",
             notes = c( "does this work?",
                        "oh {\\\\} yeah")) %>% 
  format_tt(escape = "latex") %>%
  print("latex")

The first chunk modelsummary chunk outputs the following. The \\ is correct but the % change becomes a comment instead.

\begin{table}
\centering
\begin{talltblr}[         %% tabularray outer open
entry=none,label=none,
note{}={does this work?},
note{ }={oh {\\} yeah},
]                     %% tabularray outer close
{                     %% tabularray inner open
colspec={Q[]Q[]},
column{1}={halign=l,},
column{2}={halign=c,},
hline{4}={1,2}{solid, 0.05em, black},
}                     %% tabularray inner close
\toprule
& My Model {\\} (% change) \\ \midrule %% TinyTableHeader
(Intercept) & 20.091 \\
& (1.065) \\
Num.Obs. & 32 \\
R2 & 0.000 \\
R2 Adj. & 0.000 \\
AIC & 208.8 \\
BIC & 211.7 \\
Log.Lik. & -102.378 \\
RMSE & 5.93 \\
\bottomrule
\end{talltblr}
\end{table} 

The second outputs the following. The issues are now reversed, the \\ becomes \textbackslash{} instead but the % change is formatted correctly:

\begin{table}
\centering
\begin{talltblr}[         %% tabularray outer open
entry=none,label=none,
note{}={does this work?},
note{ }={oh \{\textbackslash{}\textbackslash{}\} yeah},
]                     %% tabularray outer close
{                     %% tabularray inner open
colspec={Q[]Q[]},
column{1}={halign=l,},
column{2}={halign=c,},
hline{4}={1,2}{solid, 0.05em, black},
}                     %% tabularray inner close
\toprule
& My Model \{\textbackslash{}\textbackslash{}\} (\% change) \\ \midrule %% TinyTableHeader
(Intercept) & 20.091 \\
& (1.065) \\
Num.Obs. & 32 \\
R2 & 0.000 \\
R2 Adj. & 0.000 \\
AIC & 208.8 \\
BIC & 211.7 \\
Log.Lik. & -102.378 \\
RMSE & 5.93 \\
\bottomrule
\end{talltblr}
\end{table} 

PS \newline doesn't seem to work either because the \ still gets replaced by \textbackslash{} when using latex escape.

vincentarelbundock commented 5 months ago

Well yes, I understand this may be surprising, but you are asking tinytable to escape the content of every cell with that command. So of course the \\ will also be escaped.

One alternative would be to use the i and j arguments in format_tt() to only escape the cells that need escaping, and not the ones where you have linebreaks.

I feel this falls under "intended behavior."

writezhe commented 5 months ago

Gotcha, much appreciated! It didn't cross my mind that I could also use the i and j arguments just like in the other functions alongside the escape = option. Thanks, appreciate the time!