Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add notes over multiple lines at the end of a table #4

Closed
leifeld opened this issue Dec 2, 2016 · 9 comments
Closed

Add notes over multiple lines at the end of a table #4

leifeld opened this issue Dec 2, 2016 · 9 comments
Milestone

Comments

@leifeld
Copy link
Owner

leifeld commented Dec 2, 2016

Comment from user an ri on R-Forge:

I find myself manually adding a custom footnote after the tabular environment. The built in function custom.note puts the note into a multicolumn environment, but my annotations are often long, and the table proportions get distorted if I put it into this environment. So I add a custom note below the tabular environment using "\footnotesize". I imagine many other people do so as well.

@leifeld
Copy link
Owner Author

leifeld commented Dec 2, 2016

See also this question on StackOverflow: http://stackoverflow.com/questions/30902906/wrapping-custom-notes-in-texreg-output

@leifeld
Copy link
Owner Author

leifeld commented Dec 2, 2016

Comment by user sdaza (Sebastian Daza) on R-Forge:

Another possibility it is to add something like:

note <- paste0("\\multicolumn{", length(modnames),
"}{p{.9\linewidth}}{\\", notesize, "{", custom.note, "}}")

Instead of...

note <- paste0("\\multicolumn{", length(modnames),
"}{l}{\\", notesize, "{", custom.note, "}}")

/cc @sdaza

@MichaelChirico
Copy link
Contributor

MichaelChirico commented Dec 2, 2016

Adding my note to you over e-mail:

I'm not familiar enough with LaTeX to know how we might do so automatically, but if we add an argument like custom.note.wrap, we could change:

else {
        note <- paste0("\\multicolumn{", length(models) + 1, 
            "}{l}{\\", notesize, "{", custom.note, "}}")
        note <- gsub("%stars", snote, note, perl = TRUE)
    }

to:

if (custom.note.wrap){
  note.split <- character(ceiling(nchar(custom.note)/custom.note.wrap))
  notex <- custom.note
  for (ii in 1:length(note.split)){
    note.split[ii] <- regmatches(notex,regexpr(
      paste0(".{1,", custom.note.wrap, "}(\\s|$)"), notex))
    note<-paste0(if (ii==1) "" else paste0(note," \\ \\n"),
                 "\\multicolumn{", length(models) + 1, 
                 "}{l}{\\", notesize, "{", note.split[ii], "}}")
    notex <- substr(notex, nchar(note.split[ii])+1, nchar(notex))
  }
  note <- gsub("%stars", snote, note, perl=TRUE)
}else{
  note <- paste0("\\multicolumn{", length(models) + 1, 
                 "}{l}{\\", notesize, "{", custom.note, "}}")
  note <- gsub("%stars", snote, note, perl = TRUE)
}

The regex serves to split the string into substrings of length at most custom.note.wrap which end in a space, which we then append to the note as a separate multicolumn.

Would be great to see something like this in the official version!

A more parsimonious version takes advantage of the existing strwrap function (inspired by this):

if (custom.note.wrap){
  note<-paste(paste0("\\multicolumn{", length(models)+1L,"}{l}{\\", notesize, "{",
                     strwrap(custom.note, width=custom.note.wrap), "}}"),
              collapse = " \\ \n")

@leifeld
Copy link
Owner Author

leifeld commented Dec 3, 2016

Thanks everybody! So we've got three different ways to do it:

  1. add a minipage or something like that below the tabular environment (an ri).
  2. add a multicolumn with p{.9\linewidth} designation (@sdaza).
  3. split the note string into multiple components and add a multicolumn line for each of them separately (@MichaelChirico).

Which solution is supposed to work best? I am inclined to say number 2. But I'd welcome other views.

@leifeld
Copy link
Owner Author

leifeld commented Dec 3, 2016

Related question on StackOverflow: http://stackoverflow.com/questions/32644498/multiple-lm-texreg-export-with-second-note-line

Here, the user would like to have manual line breaks rather than automatic word wrapping.

@MichaelChirico
Copy link
Contributor

I think @sdaza's approach is bound to be most robust. Sounds like a clearly superior version of my initial idea using strwrap, which is very ad-hoc. Better to let TeX do the automated line-wrapping itself.

Perhaps just add an option to change .9 to the user's preference?

@leifeld
Copy link
Owner Author

leifeld commented Dec 4, 2016

If we use that solution, the best way is to subtract the cell margin on the left and right, rather than replacing the line width of 1.0 by 0.9: p{\dimexpr\linewidth-2\tabcolsep} (see a related StackOverflow post here).

However, that stretches the whole table to line width, even if it is actually shorter. Here is an example:

\documentclass[a4paper,10pt]{article}
\usepackage[utf8]{inputenc}

\title{}
\author{}

\begin{document}

\maketitle

\begin{table}
\begin{center}
\begin{tabular}{l c c }
\hline
 & Model 1 & Model 2 \\
\hline
(Intercept) & $5.03^{***}$ &              \\
            & $(0.22)$     &              \\
groupTrt    & $-0.37$      & $4.66^{***}$ \\
            & $(0.31)$     & $(0.22)$     \\
groupCtl    &              & $5.03^{***}$ \\
            &              & $(0.22)$     \\
\hline
R$^2$       & 0.07         & 0.98         \\
Adj. R$^2$  & 0.02         & 0.98         \\
Num. obs.   & 20           & 20           \\
RMSE        & 0.70         & 0.70         \\
\hline
\multicolumn{3}{p{\dimexpr\linewidth-2\tabcolsep}}{\scriptsize{$^{***}p<0.001$, $^{**}p<0.01$, $^*p<0.05$}}
\end{tabular}
\caption{Statistical models}
\label{table:coefficients}
\end{center}
\end{table}

\end{document}

And here is the PDF output: test2.pdf

How would we deal with that? Just letting the user figure out a width that still works seems like a kind of hackish solution to me...

/cc @sdaza

@MichaelChirico
Copy link
Contributor

MichaelChirico commented Dec 4, 2016 via email

@leifeld leifeld added this to the 1.36.25 milestone Mar 15, 2019
leifeld added a commit that referenced this issue May 8, 2020
@leifeld
Copy link
Owner Author

leifeld commented May 8, 2020

I think I have solved the problem by adding support for the threeparttable and threeparttablex LaTeX packages. See #146. This permits wrapping long notes over multiple lines and using line breaks in custom notes.

@leifeld leifeld closed this as completed May 8, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants