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

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 :)

Related

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.

Why jupyter is not able to download as pdf a markdown cell using LaTex \mathscr?

Just created a markdown cell in Jupyter using some equations, and some of them using \mathscr to have like "math" fonts. When I run the kernel containing the equations everything is ok, however when I click the option to Download as PDF via LaTex, I'm getting the error below:
! Undefined control sequence.
l.300 [\mathscr
{L}({\bf{y}}|\beta, \sigma^2, {\bf{X}}) = (2\pi\sigma^2)^{-...
?
! Emergency stop.
l.300 [\mathscr
{L}({\bf{y}}|\beta, \sigma^2, {\bf{X}}) = (2\pi\sigma^2)^{-...
If I remove the \mathscr part everything can be exported with no issues (excepting some convertion problems for special characters), however, I wanted to know ho to solve it. I've been reading and it looks like the nbconvert configuration file can be modified to solve this, but I couldn't find the mentioned file and the exact way to modify it
Thanks for your help
I think the problem is with absent \usepackage{mathrsfs} directive in an intermediate .tex-file.
So you have a several ways to overcome it.
If you face with this problem occasianaly you could the following:
download the .tex-file instead pdf;
manually insert to \usepackage{mathrsfs} to it.
before the first \usepackage for example;
run something like
xelatex file.tex to finally convert to pdf.
If you will do it often, you could try to edit appropriate jinja-template.
At first, find the place where nbconvert was installed. For example with pip: pip show nbconvert. Imagine the path is /home/i/.local/lib/python3.5/site-packages
Then the template would be at /home/i/.local/lib/python3.5/site-packages/nbconvert/templates/latex/base.tplx.
And again: just add \usepackage{mathrsfs} right after ((* block packages *)).
Voila -- the problem should gone.
At the end you have the third option -- you can create your own template from scratch and use it with nbconvert. I don't think it's very convenient way to solve your problem. You could read more in the documentation: http://nbconvert.readthedocs.io/en/latest/customizing.html

Individual function docs in LaTeX using doxygen

I'm writing a manual of sorts in LaTeX for some software. I'm constrained to use the article class. I must include docs of individual c++ classes in the manual. I'm looking at doxygen for this, but I've never used doxygen (if there's a better way, let me know).
I can get doxygen to produce a nice LaTeX (--> pdf) file of my entire project. But what I'd like to do is somehow extract the LaTeX source for each class and drop it into my LaTeX manual source at the appropriate place.
I've tried putting all the preamble stuff from refman.tex (generated by doxygen) in my preamble, and then later in the document using \input to include the tex file of an individual class. After cleaning up a few conflicts, I get to one that I don't know how to get past. The doxygen-generated doxygen.sty file makes reference to \chaptermark, but I believe that this is a macro defined in the book class.
So 1.) Am I going about this all the wrong way? 2.)Is it possible to force Doxygen to produce code in the article class? 3.) Anything else?
-garyp
You can make doxygen produce a document that uses the article document class by setting COMPACT_LATEX to YES in your doxygen config file.

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.

Odd Bibtex behaviour in a Latex document

I added a line "\cite{test}" as a test to my working Latex document. When I compiled the bibtex "!bibtex name_of_my_file, I got the expected error:
Warning--I didn't find a database entry for "test"
Then, I removed the line and compiled the bibtex again, hoping to have a working Latex file again. However, the same error occurs, even with a fresh shell. I cannot understand the behaviour. What is the logic? How can I get my Latex document working again?
[Updated Info]
The problem dissapeared as unexpectedly as it emerged. I have no idea why but it works now. Do you know the reason for the odd behaviour?
I think you are tripping over the multi-pass nature of LaTex plus Bibtex. If you look at Step 3 in this discussion, you'll see the following:
The first run (through latex)
generates an auxiliary file,
paper.aux, containing information
about citations (and other types of
references), the bibliography style
used, and the name of the bibtex
database. The second run (through
bibtex) uses the information in the
auxiliary file, along with the data
contained in the bibtex database, to
create a file paper.bbl. This file
contains a thebibliography environment
with \bibitem entries formatted
according to the bibliography style
specified.
So, what I think is happening is that your name_of_my_file.aux file still contains your placeholder \cite{test}. If you remove the auxiliary file, you should be able to start over with:
latex name_of_my_file
bibtex name_of_my_file
latex name_of_my_file
latex name_of_my_file
[Update based on additional info]: The problem was that you had a .aux file with your \cite{} still embedded. The second time that you ran latex, you overrode the old file with the new. That's why the complete set of steps includes an initial latex call, a bibtex call and two follow-up latex calls. Think of it as a multi-pass compiler and it might be more intuitive.
You could have a look at latexmk, which will take care of the fix point compilation for you.
Anyway, you should be able to build the document (pdflatex blah.tex), even if you're missing a bibliography item. The corresponding references will just appear as question marks in the PDF.
Rerun latex to regenerate the aux file.
Have a look at this discussion for pointers to a bit more information. Basically, you may have taken your citation out of the .tex file, but it still exists in one of the derived files (aux, bbl, whatever...)
Check if your bib file has the extension .bib and not .tex.
If it is .tex, just change it to .bib and that should do it.
Once I changed it accidentally to tex, by adding some references, and saving it with the "save as" option, without specifying the bib extension. That's how it can happen all of a sudden.
delete all your .aux and temporal files, re run with latex and then bibtex and then latex twice.

Resources