knitr xtable with long head note too long, stretch out the entire table - latex

I'm new to knitr and Latex stuff, so this may be a very naive question, but I failed to find a correct answer.
I'm doing empirical research that sometimes, we need to add headnotes as instruction for a complex table.
Below is the code I'm currently using (in a file named test.Rnw).
\documentclass[12pt,a4paper]{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{authblk}
\usepackage{float}
\usepackage{array}
\usepackage{booktabs}
\usepackage{mathptmx}
\usepackage{pdflscape}
\usepackage{fullpage}
\usepackage{cite}
\usepackage{color}
% packages intend to rotate table
\usepackage[graphicx]{realboxes}
\usepackage{adjustbox}
\usepackage{rotating}
\usepackage[top = 0.5in, bottom = 0.5in, left = 0.5in, right = 0.5in]{geometry}
\begin{document}
\section{TEST CODE}
<<Table_Test Test Table, echo=FALSE, results="asis", warning=FALSE, message=FALSE>>=
set.seed(138744)
df_1 <- data.frame("id" = c('a', 'a', 'a', 'b','b','b'),
'time' = c(1991,1991,1991, 2001,2003,2004),
'value' = c('x', 'y','x','z','w','u'))
table_instruction <-"This is only here to test whether the text is too long. This is only here to test whether the text is too long"
print(xtable(df_1,# first zero "represents" row numbers which we skip later
# align and put a vertical line (first "l" again represents column of row numbers)
align = paste0("ll", paste0(rep("c", dim(df_1)[2]-1), collapse = "")),
caption = "Test Table",
label='test-tab'),
floating = TRUE,
size="\\fontsize{8pt}{10pt}\\selectfont", #Change size; useful for bigger tables "normalsize" "footnotesize"
include.rownames = FALSE, #Don't print rownames
include.colnames = TRUE, #We create them ourselves
caption.placement = "top", #"top", NULL
hline.after=NULL, #We don't need hline; we use booktabs
# floating.environment = 'sidewaystable',# to rotate the table
# whether \begin{Table} should be created (TRUE) or not (FALSE)
sanitize.text.function = force, # Important to treat content of first column as latex function
add.to.row = list(pos = list(-1, -1, 0, nrow(df_1)),
command = c(paste0("\\toprule \n"),# NEW row
paste0("\\multicolumn{", dim(df_1)[2],"}{#{}l}{", table_instruction,"} \\\\" ,
"\\cmidrule(l){1-", dim(df_1)[2], "}"),
paste0("\\cmidrule(l){1-", dim(df_1)[2], "} \n"),
paste0("\\bottomrule \n \\multicolumn{",dim(df_1)[2],"}{l}",
"",paste("{\\scriptsize{", "***$p$< .001, **$p$< .01, *$p$< .05","}}",sep = " "),"\\\\"))
)
)
#
\end{document}
Then I render the above code using the code below:
rm(list=ls())
library("knitr")
knit2pdf('test.Rnw')
It works successfully, the problem is that the headnotes (right above the header row) is not self-contained to the table's width. Instead, it stretches the table way too wide.
Any suggestion on how to make the long headnotes, no matter how long, to have consistent width with the table?

Related

modelsummary output to LaTeX with automatic column width and line breaks

I estimate a model with estimatr::lm_robust and display the model output with modelsummary::modelsummary. Consider the following example:
df <- tibble(x=runif(100, 0, 100)) %>% mutate(y=x*(2+runif(100, 0, 10)), z=rep(c(1:10), 10))
tab <- modelsummary::modelsummary(
list("A very, very long title that will span multiple lines"=estimatr::lm_robust(data=df, clusters=z, y~x),
"A short title"=estimatr::lm_robust(data=df, y~x),
"A very, very long title that will span multiple lines"=estimatr::lm_robust(data=df, clusters=z, y~x),
"A short title"=estimatr::lm_robust(data=df, y~x)),
title = "This table should be on one landscape page in LaTeX with automatically adjusted column width and line breaks",
notes = c("A note that is very long and says many things and will span multiple lines where I would like automatic line breaks in LaTeX"),
output="kableExtra")
I now want to perform some kableExtra manipulations:
tab %>%
add_header_above(c(" " = 1, "M1" = 1, "M2" = 1, "M3" = 2)) %>%
row_spec(3, color = 'red') %>%
row_spec(5, background = 'lightblue') %>%
column_spec(1, width="10.5cm")
I want to ensure that the table overall has the full page width (be it landscape or portrait) and line breaks automatically included. How do I best do this?
Further, I want save to .tex. However, tab %>% ... %>% save_kable(format = "latex", file = "latex_table.tex") gives an empty file. What am I doing wrong?
Can someone point me into the right direction?
Thanks a lot.

hdashline with starting and ending markers latex

Can we have use hdashline with starting and ending markers? Something like
<---------->
One possibility is to use tikz:
\documentclass{article}
\usepackage{tikz}
\begin{document}
some text
\noindent\tikz{\draw[<->,dashed] (0,0) -- (\textwidth-0.4pt,0);}
some text
\end{document}
If you need this in a table, you can use the nicematrix package (inspired by this answer https://tex.stackexchange.com/a/635224/36296 )
\documentclass{article}
\usepackage{nicematrix,tikz}
\begin{document}
\NiceMatrixOptions
{
custom-line =
{
command = arrowrule ,
tikz = { dashed, <-> }
}
}
\begin{NiceTabular}{XX}
some & text\\
\arrowrule
some & text\\
\end{NiceTabular}
\end{document}

Biblatex: website citation is italicized and uses title instead of author when removing "n.d."

I'm trying to cite a website using the authoryear style with BibLaTeX.
\documentclass{article}
\usepackage[utf8]{inputenc}
\begin{filecontents}{ref.bib}
#online{nanopore,
author = "{Oxford Nanopore Technologies}",
title = "Oxford Nanopore Technologies Github",
url = "https://github.com/nanoporetech"
}
\end{filecontents}
\usepackage[
backend=biber,
style=authoryear,
sorting=nyt,
citestyle=authoryear,
maxbibnames=99
]{biblatex}
\addbibresource{ref.bib}
\begin{document}
\parencite{nanopore}
\end{document}
This gives me
which is fine except I want to remove the "n.d.", so then I do this to remove it:
\DeclareLabeldate[online]{%
\field{date}
\field{year}
\field{eventdate}
\field{origdate}
\field{urldate}
}
And then my citation changes, so 1) it prints the title instead of the author, and 2) the citation is italicizes (which is probably because it's printing the title instead of author).
Any idea why this is happening and how I can prevent it?
Thanks in advance!
EDIT: for #samcarter_is_at_topanswers.xyz
You could change the translation for "n.d." for the online type:
\documentclass{article}
\usepackage[utf8]{inputenc}
\begin{filecontents}{ref.bib}
#online{nanopore,
author = "{Oxford Nanopore Technologies}",
title = "Oxford Nanopore Technologies Github",
url = "https://github.com/nanoporetech"
}
\end{filecontents}
\usepackage[
backend=biber,
style=authoryear,
sorting=nyt,
citestyle=authoryear,
maxbibnames=99,
]{biblatex}
\DefineBibliographyStrings{english}{%
nodate = {\ifboolexpr{test{\ifentrytype{online}}}{\unspace}{n\adddot d\adddot}},
}
\addbibresource{ref.bib}
\begin{document}
\parencite{nanopore}
\end{document}

Using directlua command inside tikzpicture (pgf-pie) causes image not to render

Using \directlua command inside tikzpicture (pgf-pie) causes image not to render:
\documentclass[margin=5mm,preview]{standalone}
\usepackage{pgf-pie}
\begin{document}
\begin{tikzpicture} % works
\pie[radius=2, rotate=100, sum=auto]
{
3/test , 2/two , 3/three , 4/four
}
\end{tikzpicture}
\begin{tikzpicture} % does not work
\pie[radius=2, rotate=100, sum=auto]
{
\directlua{tex.sprint("3/test , 2/two , 3/three , 4/four")}
}
\end{tikzpicture}
\end{document}
The result can be view here
How gets \directlua expanded? And what do I need to change to create tikzpicture pie charts with variables from lua?

How to set bibliography text colour in beamercolor.sty file

I have made a beamer theme for my work presentations that adheres to my company's profile, however I am unable to find the code to set the colour of the bibliography text (or normal unbulleted text for that matter).
This means that the bibliography is shown as white text on the white slide. The text is there, but just in white. The same thing happens with unbulletted text on the slides.
(Additional info: I write the presentation in org-mode and compile it to a beamer pdf.)
The colour theme sty file looks like this:
\mode<presentation>
\definecolor{myblue}{RGB}{32,66,135}
\definecolor{mmconcept}{RGB}{32,66,135}
\definecolor{mmt1}{RGB}{87.75,113.25,165}
\definecolor{mmt2}{RGB}{143.5,160.5,195}
% Settings
\setbeamercolor*{title page header}{fg=myblue}
\setbeamercolor*{author}{fg=myblue}
\setbeamercolor*{date}{fg=myblue}
\setbeamercolor{item}{fg=myblue}
\setbeamercolor{frametitle}{fg=myblue}
\setbeamercolor{block title}{bg=myblue}
\setbeamercolor{block title}{fg=white}
\setbeamercolor{normal text}{fg=black}
\setbeamercolor{palette primary}{fg=myblue}
\setbeamercolor{palette secondary}{fg=myblue}
\setbeamercolor{palette tertiary}{fg=myblue}
\setbeamercolor{palette quaternary}{fg=myblue}
\mode
<all>
I have tried various settings but not seem to work.
I can change the normal text colour to a different colour by putting a latex colour tag into the slide but this doesn't work with the bibliography and I want the theme to sort this.
The colours of the bibliography are controlled by
\setbeamercolor{bibliography item}{fg=green}
\setbeamercolor{bibliography entry author}{fg=red}
\setbeamercolor{bibliography entry title}{fg=blue}
\setbeamercolor{bibliography entry location}{fg=orange}
\setbeamercolor{bibliography entry note}{fg=teal}
MWE:
\documentclass{beamer}
\usepackage{filecontents}
\begin{filecontents*}{\jobname.bib}
#book{knuth,
author = {Knuth, Donald E.},
title = {The {\TeX} book},
date = 1984,
maintitle = {Computers \& Typesetting},
volume = {A},
publisher = {Addison-Wesley},
location = {Reading, Mass.},
langid = {english},
langidopts = {variant=american},
sortyear = {1984-1},
sorttitle = {Computers & Typesetting A},
indexsorttitle= {The TeXbook},
indextitle = {\protect\TeX book, The},
shorttitle = {\TeX book}
}
#article{einstein,
author = {Einstein, A.},
title = {Die Grundlage der allgemeinen Relativitätstheorie},
journal = {Annalen der Physik},
volume = {354},
number = {7},
doi = {10.1002/andp.19163540702},
pages = {769--822},
year = {1916}
}
\end{filecontents*}
\mode<presentation>
\definecolor{myblue}{RGB}{32,66,135}
\definecolor{mmconcept}{RGB}{32,66,135}
\definecolor{mmt1}{RGB}{87.75,113.25,165}
\definecolor{mmt2}{RGB}{143.5,160.5,195}
% Settings
\setbeamercolor*{title page header}{fg=myblue}
\setbeamercolor*{author}{fg=myblue}
\setbeamercolor*{date}{fg=myblue}
\setbeamercolor{item}{fg=myblue}
\setbeamercolor{frametitle}{fg=myblue}
\setbeamercolor{block title}{bg=myblue}
\setbeamercolor{block title}{fg=white}
\setbeamercolor{normal text}{fg=black}
\setbeamercolor{palette primary}{fg=myblue}
\setbeamercolor{palette secondary}{fg=myblue}
\setbeamercolor{palette tertiary}{fg=myblue}
\setbeamercolor{palette quaternary}{fg=myblue}
\setbeamercolor{bibliography item}{fg=green}
\setbeamercolor{bibliography entry author}{fg=red}
\setbeamercolor{bibliography entry title}{fg=blue}
\setbeamercolor{bibliography entry location}{fg=orange}
\setbeamercolor{bibliography entry note}{fg=teal}
\mode
<all>
\begin{document}
\begin{frame}
\nocite{knuth}
\bibliographystyle{plain}
\bibliography{\jobname}
\end{frame}
\end{document}
In an effort troubleshoot I used the MWE supplied by user36296 and the bibliography showed with and without the colour settings.
This lead me to realise that this problem only occurs when I have my notes showing on the second screen.
Not sure if this is bug but the slides show the bibliography correctly when notes are hidden and in white when notes are on the second screen.
This is not a fix but more of a workaround, but it is the best answer for now.

Resources