Wrapping custom notes in texreg output

2020-03-21 09:35发布

I'm trying to add a fairly long note to the bottom of a table created by texreg; I want this to simply wrap around, but there doesn't seem to be any functionality built into the function for doing so.

Take, e.g.:

texreg(lm(speed~dist,data=cars),
       custom.note=paste("%stars. This regression should be",
                         "intepreted with strong caution as",
                         "it is likely plagued by extensive",
                         "omitted variable bias"))

Which, when compiled, gives something like:

simple texreg

The formatting is atrocious; much better would be something like replacing the standard output:

\multicolumn{2}{l}{\scriptsize{$^{***}p<0.001$, $^{**}p<0.01$, $^*p<0.05$. This regression should be intepreted with strong caution as it is likely plagued by extensive omitted variable bias}}

With more digestible wrapping:

\multicolumn{2}{l}{\scriptsize{$^{***}p<0.001$, $^{**}p<0.01$, $^*p<0.05$.}} \\
\multicolumn{2}{l}{\scriptsize{This regression should be intepreted with}} \\
\multicolumn{2}{l}{\scriptsize{strong caution as it is likely plagued by}} \\
\multicolumn{2}{l}{\scriptsize{extensive omitted variable bias}}

Which gives output much closer to what I'm looking for:

preferred output

Is there a way to do this programatically?

标签: r latex texreg
2条回答
beautiful°
2楼-- · 2020-03-21 10:14

I've come up with a workaround so far by rewriting the texreg function by adding a custom.note.wrap argument and changing:

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

To:

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

The idea is to pick a maximum string length for each line (custom.note.wrap) and then split the supplied note into strings of at most that length which end in a space, finally concatenating everything into a bunch of multicolumns with each split substring.

This is not optimal, as it would be better for texreg (to have the ability) to automatically set custom.note.wrap given the lengths of the model names, etc. But my raw LaTeX abilities are lacking, so I'm not sure how I would do this.

查看更多
够拽才男人
3楼-- · 2020-03-21 10:20

I might point out a neat alternative solution I received which could be of interest to you, at the latest when you need to update the texreg package.

Accordingly the custom note ends in a \multicolumn in the LaTeX code, thus we cannot use line break commands like par or \\. But we can achieve the automatic line break with \parbox. If we still want a custom line break we can use four backslashes \\\\. For better formatting we can use \\vspace{2pt} just at the beginning of the text content:

texreg(lm(speed ~ dist, data = cars),
       custom.note = ("\\parbox{.4\\linewidth}{\\vspace{2pt}%stars. \\\\
       This regression should be intepreted with strong caution as it is 
       likely plagued by extensive omitted variable bias.}"))

enter image description here

查看更多
登录 后发表回答