How can I use LaTeX package 'coffee4' to export an .Rmd to a Tufte Handout style .pdf? - latex

In this video at around 7:30 into the video, Yihui Xie make a demo of the package coffee4 which adds random coffee stains to a document. However, he is showing them in a regular LateX style pdf. I am wondering if I could also use this package or an adopted version of it when creating a pdf in the style of tufte-handout. Grateful for help on how I should go about in order to achieve such a result. In particular I am wondering how to download and install the coffee4 package in RStudio and what to write in the YAML or in R code in the document itself.

If you place coffee4.sty somewhere latex can find it, e.g. the same folder as your .rmd file, then you can use it like this:
---
title: "An Example Using the Tufte Style"
author: "John Smith"
output:
tufte::tufte_handout: default
header-includes:
- \usepackage{coffee4}
---
\cofeAm{1}{1.0}{0}{5.5cm}{3cm}
test

Related

Theorem and Proof Environment in Beamer

I am currently trying to use quarto beamer to making lecture slides. I would like to use the theorem environment in beamer, the qmd file however cannot render properly. Rendering stopped with latex error showing that Command \theorem already defined. I am wondering what have I done wrong?
The sample codes are as follows
---
title: "Untitled"
format: beamer
editor: visual
---
## Quarto
Quarto enables you to weave together content and executable code into a finished presentation. To learn more about Quarto presentations see <https://quarto.org/docs/presentations/>.
::: {#thm-test}
## Just a quick test
:::
If possible, I hope that I can include theorem, definition, example environment in quarto beamer presentation.
Thanks for the help in advance.
Quarto very unhelpfully loads tons of unnecessary packages like amsthm, which beamer automatically loads, and then blindly tries to define a new theorem environment, even though beamer already has defined it. At the very minimum it should check if an environment is already defined before doing this.
You can work around the problem with the notheorems class options.
---
title: "Untitled"
format: beamer
editor: source
classoption: "notheorems"
---
## Quarto
Quarto enables you to weave together content and executable code into a finished presentation. To learn more about Quarto presentations see <https://quarto.org/docs/presentations/>.
::: {#thm-test}
## Just a quick test
:::

natbib author-year citation - fallback to editor possible when author missing?

I'm having an issue with the natbib package. When the bib entry is missing an author I want to display the editor instead. However the second citation just takes the first three letters of the bib-key instead of the author.
Sample tex:
\documentclass{article}
\usepackage[authoryear,round]{natbib}
\bibliographystyle{abbrvnat}
\begin{document}
I need this \citet[S.~2]{Test1} to look like Surname (2022, S. 2).
and this \citep{Test1} like (Surname, 2022)
But also this \citet[S.~2]{Test2} to look like Editor (2022, S. 2).
and this \citep{Test2} like (Editor, 2022)
but it just prints (Tes, 2022)
\bibliography{refs}
\end{document}
The bib file could look like this:
#misc{Test1,
author={Surname, Name}
title = {Sometitle},
year = {2022}
}
#misc{Test2,
title = {Someothertitle},
editor={{Editor, Name}}
year = {2022}
}
Can anyone help with this? I'd also switch to another package than natbib, since I'm still early on the project. But at a first glance it seemed to meet my needs quite well.
Also I don't really know what I'm doing, it's been a while since I've been working with Latex. So sorry for that in advance!
Although the user posting the initial question here was able to switch to biblatex (which is good), not everyone has that freedom for some publishing venues. If this applies to you (that you have to stay with natbib and plainnat, you may be interested in a TeX Stack Exchange post that provides a description of how you can customize your BST file to modify your sorting, including configuring misc to sort by author then editor.
EDIT: A down-check for this answer being unhelpful inspired me to be more specific than my original answer above so the additional details specific to this case are below.
Since your provided code has natbib using abbrvnat (not plainnat as in the linked example that I initially provided), you can find abbrvnat.bst in your TeX repository and place a local copy with a different name (like abbrvnat-copy.bst) into your working directory. There are two steps in modifying your local copy of abbrvnat-copy.bst: First, update the calc.short.authors function to also look for an editor in the misc type. Second, have your TeX file use your newly customized BST file.
Note: I am using the 2007 version of abbrvnat.bst, which is the latest as of this posting in 2022.
In your local copy of abbrvnat-copy.bst, search for the calc.short.authors function. We'll need to add two lines of code to it:
type$ "misc" =
or
I show the new version here:
FUNCTION {calc.short.authors}
{ type$ "book" =
type$ "inbook" =
or
type$ "misc" =
or
'author.editor.key.label
{ type$ "proceedings" =
'editor.key.organization.label
{ type$ "manual" =
'author.key.organization.label
'author.key.label
if$
}
if$
}
if$
'short.list :=
}
In your TeX file, you'll need to change this line to account for your newly customized file: \bibliographystyle{abbrvnat-copy}. Other than this one line in your main .tex file and adding a couple missing commas in your .bib file, I did not change any code in your MWE.
Here's the output:
Other aspects that you may want to address (that you didn't ask about) include getting the editor(s) to appear in the bibliography (references list) for misc entries and sorting by editor for misc entries. The example that I provided here and the example provided in the other post that I previously linked should both be helpful in customizing your BST file to achieve your desired bibliography style.

use scholar.google.com reference in latex file

When I use scholar.google.com to get the full reference code (BibTeX) such as
#article{li2018design,
title={Design and implementation of building structure monitoring system based on radio frequency identification (RFID)},
author={Li, Hongwei and Ren, Yilei},
journal={International Journal of RF Technologies},
volume={9},
number={1-2},
pages={37--49},
year={2018},
publisher={IOS Press}
}
Then go to the journal template file, I want to copy the reference from scholar.google.com and paste it into the LaTex journal template file without doing any modification.
The template file looks like this:
Unfortunately, the paste in the template file does NOT work.
Here is the error message:
Can you assist of how to do it and make the file work correctly?
The format given by Google scholar has to be used when you compile, in order, with
Latex (or PDFLatex)
Bibtex
Latex (or PDFLatex)
Latex (or PDFLatex)
where you store the entries of your bibliography in a separate file named <mydoc>.bib.
You are simply using \thebibliography environment, which allows you to write \bibitems, which is a simpler approach (even if I suggest to use it when you have a small number of bibliography entries). If you have to use this latter approach, you have to rewrite the reference retrieved in Google scholar in the format needed by your document.
Have a look here: it explains very well the differences.

knitr/rmarkdown/Latex: How to cross-reference figures and tables in 2 different pdf files?

I'm trying to write a scientific article and the associated supplementary materials entirely in RStudio with rmarkdown.
It seems clear that book down is the way to go to cross-reference between files (https://stackoverflow.com/a/38884378/576684), but I also would like to be able to reference figures produced in one pdf in the other pdf.
Although my latex has got quite rusty with time, I imagine it could be achieved as follows:
compile the article tex and SuppMat tex a first time using rmarkdown::render()
compile these tex files from the command line in order to keep the corresponding .aux file with their references (missing references won't be resolved at this time)
recompile the 2 tex files from the command line another time where all references should now be resolved
Is it a reasonable way to do it? am I overlooking something simpler?
In any case, it requires:
a different numbering of figures in each pdf file (covered by https://stackoverflow.com/a/51337664/576684)
to prevent rmarkdown from trashing the .aux files (it seems that pandoc doesn't allow this, hence the need to create the aux file using standalone latex)
to tell latex to use the additional .aux file if it is found (probably using header-includes: in the YAML header). how can I achieve that?
Thank you very much for your help!
It turns out that the xr package is one way to go: https://texblog.org/2016/08/23/adding-references-from-an-external-file/
so this works from R:
rmarkdown::render("myarticle_ms.Rmd",
bookdown::pdf_book(base_format=rticles::plos_article),
clean=FALSE)
rmarkdown::render("myarticle_SM.Rmd",
bookdown::pdf_book(base_format=rticles::plos_article),
clean=FALSE)
tinytex::pdflatex("myarticle_ms.tex", clean=FALSE)
tinytex::pdflatex("myarticle_SM.tex", clean=FALSE)
tinytex::pdflatex("myarticle_ms.tex")
tinytex::pdflatex("myarticle_SM.tex")
with the following in the YAML header of myarticle_ms.Rmd (and the corresponding one the SuppMat file header):
header-includes:
\usepackage{xr} \externaldocument{myarticle_SM}
Hope it makes life easier for a few others :)

How to add a (large) code appendix in LaTeX / LyX?

I'd like to add a code appendix to my LyX document. There are a few options I already considered, but they all have their problems.
I know a bit about listings, but one problem with those is that, if I copy & paste my code into them, I lose all enters/newlines. Since the code is too large to correct by hand, I was wondering if there is an alternative.
In LyX there is the possibility of inserting child documents, but that seems to be only for .tex files. Would have been ideal if I could just insert my .java file as a child document.
I could print the code to PDF, but it will include margins that mess up the final document, since the PDF is placed on the left margin of the final document and then there is the margin of the PDF. Also, this PDF always contains the entire code and white areas where not the entire page has been filled.
Does anyone have good alternative?
The listings package found here
http://www.ctan.org/tex-archive/macros/latex/contrib/listings/
allows the include of external source code files (look into the reference for \lstinputlisting).
EDIT: here you find some samples how to use it:
http://en.wikibooks.org/wiki/LaTeX/Packages/Listings
If you need to copy-paste code to LyX listing box then use Edit -> Paste Special -> Seletion or Ctrl+Alt+V.
For what it's worth, at least the 2.0 versions of LyX have the ability to include listings as child documents. Insert, File, Child Document, and choose from the dropdown box "Program Listing". This uses the listings package and lets you keep your source in its own file.
If listings doesn't support your language, you can always use something like highlight or source-highlight to generate a latex snippet of syntax-highlighted code that you can add as a child document of type "Input"
Yes, if you copy&paste code into the LyX listings box, you lose all newlines, but you can preprocess your code (insert an extra newline below each line):
$ cat foo.java | sed -e 's/$/\n/' > bar.java
Then you can copy&paste the new file bar.java and everything will be ok.

Resources