Synchronise pdf to Rnw in Knitr with texshop - latex

Would anyone know how to get synctex to work from the pdf to the Rnw in knitr with texshop? It does work from Rnw to pdf. Many thanks.

This is how I worked this out. Not tried on multiple .Rnw files.
In TeXShop Preferences, make sure your "Sync Method" is set as "SyncTeX (TeX ≥ 2010)".
On your Mac, make the directory "~/Library/TeXShop/Rscripts" and put the R file "patchKnitrSynctex.R" downloaded from https://github.com/jan-glx/patchKnitrSynctex in this directory.
Create an executable file "Knitr.engine" including the following shell scripts and put it in "~/Library/TeXShop/Engines/":
#!/bin/bash
# export PATH=$PATH:/usr/texbin:/usr/local/bin # already on my path!
Rscript -e "library(knitr); knit('$1')"
latexmk -pdf -pdflatex='pdflatex -shell-escape -synctex=1 -file-line-error' "${1%.*}"
Rscript -e "source('~/Library/TeXShop/Rscripts/patchKnitrSynctex.R', echo=FALSE, encoding='UTF-8'); patchKnitrSynctex('${1%.*}')"
In R, install the package "patchDVI".
In your .Rnw file, add "% !TEX TS-program = Knitr" on the top line of the document. Also inside the .Rnw document somewhere around the top of the document add an R code chunk
<<setup, include=FALSE>>=
patchDVI::useknitr()
… #any other knitr global setups
#
Happy knitting!

Related

Compile two versions of a document from the same latex source

How to automatically compile two versions of the same document, for example version without answers for the students and another version with answers for myself?
I have a small bash script to do a dual format.
function latex-ans () {
n=$(basename $1 .tex) # strip .tex in filename foo.tex -> foo
ln -s $n.tex $n-ans.tex # create a soft link (for instance foo-ans.tex -> foo.tex)
pdflatex '\def\withanswer{1} \input{'$n-ans'}' && pdflatex $n
% first format the version with answers and then the normal version
rm $n-ans.tex $n-ans.log
% remove useless files
}
If I have a file foo.tex, this commands formats both versions of the file and generates two pdf: foo.pdf and foo-ans.pdf. Thanks to the renaming of foo.tex through the ln -s, it also keeps separate foo.aux and foo-ans.aux to preserve useful information on both versions.
At the latex level, I basically do the same and use the macro \withanswers to configure my packages.
There are several packages that allow to conditionally exclude certain parts of the document, for example the exercise package.
With TeXstudio, the following magic comment can be used to automatically compile both versions at once (including repeated compilation for cross-references, bibliographies, indices etc.):
% !TeX program = latexmk -pdf -pdflatex="pdflatex -synctex=1 -interaction=nonstopmode -shell-escape" -jobname=% -pretex="\newcommand{\version}{noanswer}" -usepretex % | latexmk -pdf -pdflatex="pdflatex -synctex=1 -interaction=nonstopmode -shell-escape" -jobname=%_solution -pretex="\newcommand{\version}{}" -usepretex % | txs:///view-pdf "?am)_solution.pdf"
\documentclass{article}
% setting a default value in case it is compiled without the magic comment
\ifdefined\version
\else
\def\version{noanswer}
\fi
\usepackage[\version]{exercise}
\begin{document}
\begin{Exercise}[title={Title},label=ex1]
question text
\end{Exercise}
\begin{Answer}[ref={ex1}]
solution
\end{Answer}
\end{document}

Run LaTeX command in Sublime 3

I'm working with LaTeX files in Sublime 3 (3059). Given a main .tex file and a .bib file holding the citations, I'd like to be able to issue the standard:
pdflatex main.tex && bibtex main.aux && pdflatex main.tex && pdflatex main.tex
command after hitting a given shortcut in Sublime.
I know there are tools that can do this (LatexTools, LaTeXing) but since what I need is (I believe) so simple, I'd prefer not to depend on extra packages.
How can I store this command and have Sublime run it following a given key combination?
Edit: Based on the answer given, this is the full command I ended up using:
{
"cmd": ["pdflatex $file_name && bibtex $file_base_name.aux && pdflatex $file_name && pdflatex $file_name"],
"shell": true
}
If you want to execute a command you could use Tools > Build System > New Build System to create a file with a content similar to this one:
{
"cmd": ["command"]
}
And then, you should select that as your project Build System. Finally, use ctrl+b to invoke build (invoke your command or script).

Markdown to PDF using Pandoc since Xetex Deprecation

On my MacBook (OSX Mountain Lion), I used to use this Pandoc command to convert Markdown to PDF:
$ markdown2pdf -N -o pandoc_output.pdf --xetex --toc --template=mytemplate.tex myfile.md
But markdown2pdf no longer works, and --xetex option in markdown2pdf -N -o ../../Desktop/pandoc_output.pdf --xetex --toc --template=mytemplate-headers-garamond_date.tex is deprecated.
If I do this:
$ pandoc -N -o Desktop/pandoc_output.pdf --xetex --toc --template=mytemplate.tex myfile.md
I get this:
pandoc: unrecognized option `--xetex'
But if I take out --xetex and do this:
$ pandoc -N -o Desktop/pandoc_output.pdf --toc --template=mytemplate.tex myfile.md
then I get this:
pandoc: Error producing PDF from TeX source.
! Package hyperref Error: Wrong driver option `xetex',
(hyperref) because XeTeX is not detected.
See the hyperref package documentation for explanation.
Type H <return> for immediate help.
...
l.3925 \ProcessKeyvalOptions{Hyp}
What's the solution?
Try --latex-engine=xelatex instead of --xetex
The prior answers to this question were helpful to me, as I had installed pandoc a couple years ago, but never Tex Live. Consequently I had no idea if I had installed it correctly, so putting in the entire path helped me to see that it was working, as follows:
pandoc --latex-engine=/usr/local/texlive/2012basic/bin/universal-darwin/xelatex
This is the default install location for the BasicTex setup which you download from the Pandoc install page.
I had also forgotten about using pandoc -D Latex >my-latex-template.tex to generate a template. After giving a .tex template instead of my .html one (which caused a 'you don't have BEGIN {' error) , I got .PDF: In other words, the default template worked.
Also, I had inaccurately entered -t pdf (not shown above) to set pdf as an output format, but this was not correct. The output format is Latex, which is then translated to PDF. It is not necessary to specify an output format with the dash -t option.
I hope this record of my minor stumbles saves someone some time.
See the pandoc User's Guide (or man page) for the --latex-engine option.

Using Sphinx docs how can I specify png image formats for HTML builds and pdf image formats for Latex/PDF builds?

Using sphinx doc generator, I am trying to use .png images for the HTML builds of the documentation, and then I want to have the .svg images used for the PDF/LATEx builds.
Anyone know how to "tag" sections as "HTML build"-only and "Latex build"-only?
Cheers
Take a look at these options:
Image filename wildcard:
.. image:: gnu.*
From the documentation: "For instance, if the file name gnu.* was given and two files gnu.pdf and gnu.png existed in the source tree, the LaTeX builder would choose the former, while the HTML builder would prefer the latter."
The only directive:
.. only:: latex
This appears only in LaTeX output.
.. only:: html
This appears only in HTML output.
It is possible to use the makefile to automatically build the appropriate output formats.
A tutorial demonstrating a similar process for using Sphinx with SVG and LaTeX PDF output is also available.
Use the image filename wildcard option in your .rst source.
.. image:: my_image.*
Use Inkscape to convert your source images into PDFs and PNGs at build-time. You can do this automatically at build-time by adding the following code to your Makefile:
SOURCEDIR = source
#IMAGEDIRS can be a list of directories that contain SVG files and are relative to the SOURCEDIR
IMAGEDIRS = _images
# SVG to PDF conversion
SVG2PDF = inkscape
SVG2PDF_FLAGS = -C
# SVG to PNG conversion
SVG2PNG = inkscape
SVG2PNG_FLAGS = -C -d=90 --export-background-opacity=\#00
# Pattern rule for converting SVG to PDF
%.pdf : %.svg
$(SVG2PDF) $(SVG2PDF_FLAGS) -f $< -A $#
# Pattern rule for converting SVG to PNG
%.png : %.svg
$(SVG2PNG) $(SVG2PNG_FLAGS) -f $< -e $#
# Build a list of SVG files to convert to PDFs
PDFs := $(foreach dir, $(IMAGEDIRS), $(patsubst %.svg,%.pdf,$(wildcard $(SOURCEDIR)/$(dir)/*.svg)))
# Build a list of SVG files to convert to PNGs
PNGs := $(foreach dir, $(IMAGEDIRS), $(patsubst %.svg,%.png,$(wildcard $(SOURCEDIR)/$(dir)/*.svg)))
# Make a rule to build the PDFs
images-pdf: $(PDFs)
# Make a rule to build the PNGs
images-png: $(PNGs)
# Make a rule to build the images
images: images-pdf images-png
clean-pdf:
-rm $(PDFs)
clean-png:
-rm $(PNGs)
clean-images: clean-pdf clean-png
Finally, update the clean, latex and latexpdf rules to have a dependency on the respective image targets:
...
clean: clean-images
...
html: images-png
...
latex: images-pdf
...
latexpdf: images-pdf
...
Now you can build your images by typing make images and clean them with make clean-images. Using make html, make latex and make latexpdf will automatically make sure your images are up-to-date.
One problem is that Sphinx defaults to preferring SVG over PNG in HTML output. You can fix this by overriding preferece in your conf.py file.
Add the following lines near the top of your conf.py file, after imports.
# Redefine supported_image_types for the HTML builder
from sphinx.builders.html import StandaloneHTMLBuilder
StandaloneHTMLBuilder.supported_image_types = ['image/png', 'image/svg+xml',
'image/gif', 'image/jpeg']

Convert xypic matrix to eps. (LaTeX)

I have a lot of BIG xypic-matrices in my LaTeX file for one of my papers, and it takes long/infinite time to compile. Is there a way to convert just the separate xypic-pieces to eps files, that I include in my main document?
I suppose I use LaTeX to compile the xypic part, but then I will get an entire document, right?
(Can someone add the tag xypic to this question?)
Update: The solution I came up with was to have separate LaTeX documents for each xymatrix, and then use dvips -E to capture the figure. I created a script for Compiling + Create eps in Kile, the editor I use.
Use latex to compile, then a
dvips -E -o temp.eps temp.dvi
to make an eps out of it, then epscrop to make a small eps you can include later.
There are a few ways to run Latex on just a portion of a document; the most widely used is likely Emacs/Auctex's TeX-command-region (C-c C-r), which generates a temporary Latex file whose dvi/eps/pdf/whatever output is just that from the selected region.
You can use the preview package to only process your pictures. For example:
$ cat a.tex
\documentclass{article}
\usepackage[active,tightpage]{preview}
\setlength\PreviewBorder{5pt}
\usepackage [arrow]{xy}
\begin{document}
\begin{preview}
\begin{xy}
(0,-20)="a", (0,0)="b"
\ar#{<.||} #<24mm> "a";"b"
\ar#^{<.||} #<16mm> "a";"b"
\ar#_{<.||} #<8mm> "a";"b"
\ar#0{<.||} "a";"b"
\ar#1{<.||} #<-8mm> "a";"b"
\ar#2{<.||} #<-16mm> "a";"b"
\ar#3{<.||} #<-24mm> "a";"b"
\end{xy}
\end{preview}
\end{document}
Then, when you run pdflatex, preview will generate a pdf with only the pictures in it. You can convert them to eps by ghostscript:
$ latex a
$ dvips -E -i -Pwww -o figure.000 a
or you can generate PNG images:
$ pdflatex a
$ gs -dNOPAUSE -r400 -dGraphicsAlphaBits=4 -dTextAlphaBits=4 \
-sDEVICE=png16m -sOutputFile=figure.png -dBATCH a.pdf
Then, you can include the generated files in your document.

Resources