RSweave: How to copy/paste from pdf to notepad avoiding error? - r

I compile a latex document which contains accents ("é" for instance) using RSweave. Then I copy the text from the pdf generated by pdflatex in a notepad. The resulting shows strange characters, for some encoding reason.
Is there a way to solve this problem?
Minimal working example
\documentclass{article}
\usepackage[utf8]{inputenc}
\begin{document}
\SweaveOpts{concordance=FALSE}
Orçamentária \\
Tendência \\
saída \\
gestão \\
órgão \\
coração
\end{document}
When I copy this words from pdf to notepad (pdf open in Adobe Reader), I get:
Orcamentaria
Tend^encia
sada
gest~ao
org~ao
corac~ao
The real output

After search a lot, I discover the source of problem. In C:\Users...\Documents\R\R-3.1.2\share\texmf\tex\latex\Sweave.sty there is:
\ifthenelse{\boolean{Sweave#ae}}{%
\RequirePackage[T1]{fontenc}
\RequirePackage{ae}
}{}%
According to https://en.wikibooks.org/wiki/LaTeX/Fonts :
The package ae (almost European) is obsolete. It provided some workarounds for hyphenation of words with special characters. These are not necessary any more with fonts like lmodern. Using the ae package leads to text encoding problems in PDF files generated via pdflatex (e.g. text extraction and searching), besides typographic issues.
So, if I comment this line in Sweave.sty I solve the problem. Or just comment \userpackage{sweave} in .tex file.

Related

Escaping backslash in R Sweave document causing line break

I want to write something in an R Sweave document so that the following appears in the compiled PDF:
When you write "\beta" and compile the document, you see "β".
However, when I escape the backslash for beta, I'm getting a line break, which is not desired.
How do I get "\beta" rather than a line break?
Example from file test.rnw:
\documentclass{article}
\begin{document}
When you write ``\\beta'' and compile the document, you see ``$\beta$''.
\end{document}
Result:
Setup:
Macbook Pro, R 4.1.0, RStudio 1.4.1717, rmarkdown 2.9, and some version of MacTex installed. RStudio is set to compile using knitr and pdflatex.
The double blackslash in text mode induces a linebreak (see here for a simple explanation).
You may use also verb"\beta" to better distinguish the Latex command from normal text.
When you write \verb"\beta" and compile the document, you see $\beta$.
Use \textbackslash
When you write ``\textbackslash beta'' and compile the document, you see ``$\beta$''.

knitr Minimal Demo not compiling correctly

Hi knitr experts: I cannot for the life of me figure out how to compile the knitr Miminal Demo .Rnw document correctly. When I download and run Yihui's Minimal Demo of knitr .Rnw file (link), the document compiles but incorrectly handles the R chunks:
I changed nothing, just opened and compiled. Help welcome. As an aside, would very much appreciate tips on whether this is how I should be going about adding R code into a latex template that UT-Austin requires for dissertation publication. Thanks.
I can reproduce your PDF file when using Sweave instead of knitr for translating the .Rnw file. When switching to knitr via the RStudio options (c.f. https://support.rstudio.com/hc/en-us/articles/200532247), I get an error message and no PDF, which probably reproduces #r2evans' results in the comments. I also get a warning message that line 19 is Sweave specific (\SweaveOpts{concordance=TRUE}). Removing that line, the file processes with knitr correctly producing

Cannot compile moderncv class document when using tinytex in RStudio

I am unable to compile an Rmd file using tinytex via RStudio. I can compile the same file without issues on another computer that has a full LaTeX installation.
I have written my CV using the moderncv class in RMarkdown. I have a full LaTeX distribution installed on one of my computers and I can compile the CV without any problems. On my new computer I have installed TinyTex via the r package tinytex and use it to compile Rmd files to pdf via RStudio. For some reason I cannot compile the same document on my new computer when using tinytex. I have tried manually installing all of the packages I load in my preamble without luck. My preliminary searches have not uncovered in problems similar to this one. I am using the kjh-vita template provided by Kieran Healy, though it has been modified to work with Rmd.
I've noticed that if I set the keep_tex YAML argument to yes that the .tex file that is produced is escaping out all of the curly braces (see image). The green portion is how it should look (i.e., how it looks in my .Rmd file) and the red portion is what is produced in the .tex file after knitting the document.
I believe the solution to my problem involves getting tinytex to stop escaping all of the curly braces in the file. I have no idea why this is happening or how to get it to stop.
update: add minimal working example
% Basic preamble
\documentclass[11pt,letterpaper]{moderncv}
% Pull from header includes
% moderncv themes
\moderncvtheme[black]{classic} % optional argument are 'blue' (default),
% 'orange', 'green', 'red', 'purple',
% 'grey' and 'roman' (for roman fonts,
% instead of sans serif fonts)
%\usepackage{assets/tex/moderncvthemeclassic}
%\usepackage[utf8]{inputenc} %character encoding
%\usepackage{hyperref}
\usepackage{url,fancyhdr}
% needed for xelatex to work
\usepackage{fontspec}
\usepackage{xunicode}
\usepackage{xcolor}
% my additions:
% include ipa and bib formatting
\let\sups\relax
\usepackage{tipa}
\usepackage{multicol}
\usepackage[spanish, english]{babel}
\usepackage{natbib}
\usepackage{bibentry}
\newcommand{\bibverse}[1]{\begin{verse} \bibentry{#1}. \end{verse}}
\usepackage{hanging}
\usepackage{fontawesome5}
\usepackage{academicons}
% adjust the page margins
\usepackage[scale=0.8]{geometry}
%\setlength{\hintscolumnwidth}{3cm} % if you want to change the
% width of the column with the
% dates
%\setlength{\maketitlenamewidth}{10cm}} % only for the classic theme,
% if you want to change the
% width of your name placeholder
% (to leave more space for your
% address details
% Personal data
\firstname{Person}
\familyname{Name}
\title{Assistant Professor}
\address{Some University \\
Department of Stuff}{City, State 08401}
% \mobile{}
\phone{(456) 921-6345}
%\fax{(456) 921-6345}
\email{anemail#mail.edu}
\homepage{www.page.com}
% \extrainfo{}
\def\mytitle{Professor}
\def\myphone{(456) 921-6345}
\def\myskype{myskype}
\def\myemail{jmymail#mail.edu}
\def\myweb{www.page.com}
\def\facweb{https://www.page.com}
\def\school{Some University}
\def\schoolweb{http://www.page.edu}
%\nopagenumbers{} % uncomment to suppress automatic page numbering for CVs
% longer than one page
\begin{document}
\bigskip
\vspace{.1in}
\noindent{\huge {\textsc{My Name}, Ph.D.}}
\reversemarginpar
\noindent
\rule{492pt}{1.5pt}
\vspace{-0.1in}
\begin{minipage}[t]{2.4in}
\flushleft
\href{\schoolweb}{\school} \\
My buildin \\
15 Street place \\
Street, \textsc{State} 08305
\end{minipage}
\hfill
\begin{minipage}[t]{1.7in}
\flushleft
\faUniversity \phantom{-}\href{\facweb}{Faculty page} \\
\faPhone \phantom{-}\myphone \\
\faPaperPlane \phantom{-}\href{\myemail}{\texttt{\myemail}} \\
\faHome \phantom{-}\href{https://www.page.com}{\texttt{\myweb}} \\
\end{minipage}
\hfill
\begin{minipage}[t]{1.7in}
\flushright
Google scholar: \href{http://www.page.com}{\aiGoogleScholar} \\
ResearchGate: \href{http://www.page.com}{\aiResearchGate} \\
#jvcasillas \href{https://www.github.com/}{\faGithub} \\
#jvcasill \href{https://www.twitter.com/}{\faTwitter}
\end{minipage}
\vspace{0.1in}
\section{Appointments}
\cventry{2016 -- Present}
{\mytitle}
{Some University, Place}
{}
{}
{Name of program,
Department of stuff}
\end{document}
After pandoc version 2.5 the \cventry{} lines are not correctly parsed into latex. Specifically, this was what was causing the problems:
\cventry{2016 -- Present}
{\mytitle}
{Some University, Place}
{}
{}
{Name of program,
Department of stuff}
I can knit the document that contains the above code with pandoc 2.5. Any version above 2.5 must be written as follows:
\cventry{2016 -- Present}{\mytitle}{Some University, Place}{}{}{Name of program, Department of stuff}
Pandoc inserts an escape character \{\} whenever there is blank space (as seen in the image above).

Bookdown: Fix extra space before Chinese string inside R code chunk

When Chinese string inside R code chunk, the compiled PDF will get a redundancy space before the string, how to avoid this extra space? Please refer the minimum case in github - bookdown-chinese .
This issue was caused by the LaTeX package xeCJK. By default, it adds spaces between Chinese and non-Chinese characters, except in verbatim environments. In your case, the code was not actually in a verbatim environment, so you have to let xeCJK know that it should not add spaces automatically.
The solution is to add this line to your LaTeX preamble (the Highlighting environment was defined by Pandoc when converting Markdown to LaTeX to syntax highlight code, and it is based on the fancyvrb package):
\RecustomVerbatimEnvironment{Highlighting}{Verbatim}{commandchars=\\\{\},formatcom=\xeCJKVerbAddon}
For R Markdown documents, this line can be save in a .tex file, e.g., preamble.tex, and included via the includes option, e.g.,
output:
pdf_document:
includes:
in_header: preamble.tex
See this Github issue for the full technical background.

knitr html to Word docx using pandoc

I have been saving some example R markdown html output to Word using pandoc. I actually only do this so I can add some page breaks for easier printing:
system("pandoc -s Exercise1.html -o Exercise1.docx")
Although the output is acceptable I was wondering if there is a way to keep the original syntax highlighting of the R chunks (just as they are in the original knit HTML document)?
Also, I seem to be loosing all images in the conversion process and have to stick them into Word by hand. Is that normal?
Using the rmarkdown package (baked into RStudio Version 0.98.682, the current preview release) it's very simple to convert Rmd to docx, and code highlighting is included in the docx file.
You just need to include this at the top of your markdown text:
---
title: "Untitled" # obviously you can change this
output: word_document # specifies docx output
---
However, it seems that page breaks are still not supported in this conversion.
Why not convert the markdown directly to Word format?
Anyway, Pandoc does not support syntax highlighting in Word: "Currently, the only output formats that uses this information are HTML and LaTeX."
About the images: the Word file would definitely include those if you'd convert the markdown to Word directly. I am not sure about the HTML source, but I suppose you might have a path issue.

Resources