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

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.

Related

Unable to make sense out of "Rstudio pdf knit fails with 'Environment Shaded undefined' error" [duplicate]

This question already has answers here:
Rstudio pdf knit fails with "Environment Shaded undefined" error
(5 answers)
Closed 4 years ago.
I am creating a report using the rticles package and I get the following error when knitting an rticle IEEE paper which includes code-chunks:
output file: rticles_IEEE.knit.md
! LaTeX Error: Environment Shaded undefined.
I tried to understand how to fix the issue and played around with this question/answer. But I fail to see where to put the fix into the files created by knitting (e.g. mydoc.tex, IEEE_trans.cls).
Here is my minimal example:
---
title: Cool IEEE ArticleS
author:
- name: Ray
abstract: |
The abstract of my first IEEE paper with rmarkdown
output: rticles::ieee_article
---
Introduction
=============
This is the introduction. And here comes a cool code chunk.
```{r}
x <- seq(-3,3,0.1)
y <- x^3
plot(x,y)
```
Notes:
It was commented that this is a potential duplicate of another question. The problem with the answers given in that thread is that it does not explain where to make the changes.
I fail to understand the pointer to Yihui's fix.
The aforementioned files, but also the knitted .tex does not come with labels of the form \makeatother or \makeatletter.
The overall issue was that displaying a code chunk in the rticles IEEE paper throws an error pointing at the undefined Shaded environment.
The solution given in the older post works, if you add the following to the underlying LaTeX template.tex. My problem was that I tried to edit the tex or cls template in the working directory. The Eureka-moment came when I stumbled over the following (related) stackoverflow question.
For the rticles package, the template.tex is not located in your directory you work on the article R Markdown file. Instead, they are stored within the package library folder (i.e. the folder that .libPaths() uses). The file can be found in rmarkdown/templates/ and search for the rticles folder you are using (in my case: ieee_article). In the resources sub-folder you will find the target template.tex. Make a copy of it, for recovery purposes.
Open the template.tex and insert the following anywhere before the LATEX command line \begin{document} (note: aka the preamble of that template.tex file). For example, I inserted it just before the \begin{document} line:
% code to insert to fix environment Shaded undefined issue with
% showing code chunks in rticle IEEE template.
$if(highlighting-macros)$
$highlighting-macros$
$endif$
\begin{document}
Save the change and go back to R/RStudio and your IEEE paper R Markdown file and hit the knit button. R code chunks will now be displayed (if you want to).
I have not tested it thoroughly, but this recipe should work for all rticle templates missing an environment definition for showing r-code chunks (i.e. environment{Shaded})
Rather than duplicating the template, you can put the required code for allowing the highlighting in a separate header.tex file. This is essentially what replaces $highlighting-macros$ when you include this in the template:
header.tex
\usepackage{color}
\usepackage{fancyvrb}
\newcommand{\VerbBar}{|}
\newcommand{\VERB}{\Verb[commandchars=\\\{\}]}
\DefineVerbatimEnvironment{Highlighting}{Verbatim}{commandchars=\\\{\}}
% Add ',fontsize=\small' for more characters per line
\usepackage{framed}
\definecolor{shadecolor}{RGB}{248,248,248}
\newenvironment{Shaded}{\begin{snugshade}}{\end{snugshade}}
\newcommand{\KeywordTok}[1]{\textcolor[rgb]{0.13,0.29,0.53}{\textbf{#1}}}
\newcommand{\DataTypeTok}[1]{\textcolor[rgb]{0.13,0.29,0.53}{#1}}
\newcommand{\DecValTok}[1]{\textcolor[rgb]{0.00,0.00,0.81}{#1}}
\newcommand{\BaseNTok}[1]{\textcolor[rgb]{0.00,0.00,0.81}{#1}}
\newcommand{\FloatTok}[1]{\textcolor[rgb]{0.00,0.00,0.81}{#1}}
\newcommand{\ConstantTok}[1]{\textcolor[rgb]{0.00,0.00,0.00}{#1}}
\newcommand{\CharTok}[1]{\textcolor[rgb]{0.31,0.60,0.02}{#1}}
\newcommand{\SpecialCharTok}[1]{\textcolor[rgb]{0.00,0.00,0.00}{#1}}
\newcommand{\StringTok}[1]{\textcolor[rgb]{0.31,0.60,0.02}{#1}}
\newcommand{\VerbatimStringTok}[1]{\textcolor[rgb]{0.31,0.60,0.02}{#1}}
\newcommand{\SpecialStringTok}[1]{\textcolor[rgb]{0.31,0.60,0.02}{#1}}
\newcommand{\ImportTok}[1]{#1}
\newcommand{\CommentTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textit{#1}}}
\newcommand{\DocumentationTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textbf{\textit{#1}}}}
\newcommand{\AnnotationTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textbf{\textit{#1}}}}
\newcommand{\CommentVarTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textbf{\textit{#1}}}}
\newcommand{\OtherTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{#1}}
\newcommand{\FunctionTok}[1]{\textcolor[rgb]{0.00,0.00,0.00}{#1}}
\newcommand{\VariableTok}[1]{\textcolor[rgb]{0.00,0.00,0.00}{#1}}
\newcommand{\ControlFlowTok}[1]{\textcolor[rgb]{0.13,0.29,0.53}{\textbf{#1}}}
\newcommand{\OperatorTok}[1]{\textcolor[rgb]{0.81,0.36,0.00}{\textbf{#1}}}
\newcommand{\BuiltInTok}[1]{#1}
\newcommand{\ExtensionTok}[1]{#1}
\newcommand{\PreprocessorTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textit{#1}}}
\newcommand{\AttributeTok}[1]{\textcolor[rgb]{0.77,0.63,0.00}{#1}}
\newcommand{\RegionMarkerTok}[1]{#1}
\newcommand{\InformationTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textbf{\textit{#1}}}}
\newcommand{\WarningTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textbf{\textit{#1}}}}
\newcommand{\AlertTok}[1]{\textcolor[rgb]{0.94,0.16,0.16}{#1}}
\newcommand{\ErrorTok}[1]{\textcolor[rgb]{0.64,0.00,0.00}{\textbf{#1}}}
\newcommand{\NormalTok}[1]{#1}
Make a copy of this file in your directory and then refer to this header in the YAML:
---
title: Short Paper
output:
rticles::ieee_article:
includes:
in_header: header.tex
---
# Introduction
This is the introduction. And here comes a cool code chunk.
```{r}
x <- seq(-3,3,0.1)
y <- x^3
plot(x,y)
```
This approach should work for any template which includes the $header-includes$ tag in thetemplate.tex`

Rmd to PDF compiling error: Package geometry \paperwidth (0.0pt) too short

I am writing a paper in R markdown and need to format it with this .cls file supplied by an academic journal.
A minimal .tex file compiles perfectly well with the above cls file.
My .tex file (compiled on ShareLaTeX with clv3.cls saved in same directory):
\documentclass[shortpaper]{clv3}
\usepackage[utf8]{inputenc}
\title{Paper title}
\author{Name Surname}
\date{May 2018}
\begin{document}
\maketitle
\section{Introduction}
Some text.
\end{document}
However a comparable minimal document in R markdown, using the same cls file, fails to compile in Rstudio, with the following error: ! Package geometry Error: \paperwidth (0.0pt) too short.
My Rmd file (with clv3.cls file saved in same directory):
---
title: "Paper title"
author: "Name Surname"
documentclass: clv3
classoption: shortpaper
output: pdf_document
---
# Introduction
Some text.
Why is this error induced when I try to use this class file with an R markdown document and how may I fix it?
I've tried manually specifying a pagewidth setting in the YAML header, but I don't really know what I'm doing. This seems undesirable anyway, since the normal LaTeX document works fine without it (and surely page width is something that should be specified by a journal, not manually overwritten by an author).
I do not know where exactly the clv3.cls class and the default pandoc template clash. However, that template does so many things that do not make sense when writing with a specific style, that is best to use your own template. Using clv3-template.tex
\documentclass[shortpaper]{clv3}
\usepackage[utf8]{inputenc}
$if(title)$
\title{$title$}
$else$
\title{}
$endif$
$if(author)$
\author{$for(author)$$author$$sep$ \\ $endfor$}
$else$
\author{}
$endif$
\begin{document}
$if(title)$
\maketitle
$endif$
$body$
\end{document}
together with
---
title: "Paper title"
author: "Name Surname"
output:
pdf_document:
template:
clv3-template.tex
---
# Introduction
Some text.
should be a good starting point.
The accepted answer works perfectly for the minimal example presented. However it breaks again pretty quickly as the document is made more complex (for example, inserting a bibliography and in-text citations). I'd like to expand a little on my solution for the potential benefit of any future readers, as I found it a bit of steep learning curve:
The issue here is that Pandoc has a LaTeX template, which it uses to produce PDF documents. This is separate from a .cls class file, which defines a document class. Like Ralf Stubner says, something about my particular class file was not cooperating with Pandoc's default template. This is probably super basic and obvious to many, but I had not appreciated this extra step nor understood the distinction between these files.
If one does not wish to deal with raw LaTeX, it seems there are quite a few templates out there for various kinds of documents (see, for example, the rticle package). Using R Markdown to produce a PDF document in a particular custom format (such as for a particular journal) will require construction of a LaTeX template, however. This can be done by either of two ways:
Tinkering with an existing template until you get what you need, either by finding Pandoc's default template and starting from there (see comment by user2554330 for location) or by cloning someone else's template on Github etc.
Writing a template from scratch. In this case, Ralf Stubner's minimal example above plus this section of the Pandoc manual will be informative.
In my case, I went with the latter option. I have saved my eventual template as an R package which can be installed using devtools::install_github("JaydenM-C/CLtemplate"). So if anyone else would ever like to author a document for Computational Linguistics using this particular document style, this may save you some time.

Two space line break doesnt work on GitHub when converting .rmd to .md using github_document

I was trying to convert an .rmd document to .md to upload on GitHub using output: github_document in my YAML header.
Apparently, the conversion does not respect the two or more spaces I used in my .rmd file to force a linebreak.
Example: I created this little .rmd file:
---
title: "Line Break issue"
output: github_document
---
1. My First point
Another point right below
and more
1. My second point
I used two spaces to force a break between "My first Point" and "Another point..." and "and more". When I knit, the preview is fine, however, when I upload to GitHub the linebreaks are only adequately respected in the .rmd file as you can see here but apparently gone in the .md file as you can here.
Why is that?
EDIT:
I am using rmarkdown_0.9.6 within RStudio Version 0.99.1130 on R 3.3.0.
It looks to me as if the behavior of GitHub's markdown renderer has changed. Formerly GitHub Flavored Markdown automatically created an HTML line break for any line break within Markdown (even those without 2 trailing spaces). As a result the pandoc conversion to github_markdown stripped off the 2 trailing spaces (as they were not necessary or meaningful in github_markdown).
Posting some test files on GitHub today however it looks like they now require the two spaces to yield a line break. I don't know if this change is permanent or temporary (as it will be sure to break a bunch of existing code). To deal with this I've added a hard_line_breaks option to github_document:
https://github.com/rstudio/rmarkdown/commit/0cbfcf7ebde5f587e70a064ae7c484cfd849ba6a
With this change the YAML of your example would need to look like this to preserve the two-spaces:
---
title: "Line Break issue"
output:
github_document:
hard_line_breaks: false
---
If this appears to be a permanent change by GitHub then I'll change the default to true so github_document will produce the right markdown by default.

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.

centre title in PDF converted from markdown using Pandoc

I'm converting a markdown document to a PDF document using Pandoc from within R. I'm trying to centre the title.
So far, I've tried:
<center># This is my title</center>
and
-># This is my title<-
but neither have worked. Is there a way to centre the title when converting from markdown to PDF using Pandoc?
pandoc has its own extended version of markdown. This includes a title block.
If the file begins with a title block
% my title
% Me; Someone else
% May 2013
This will be parsed into LaTeX and the resulting pdf as
\title{my title}
\author{Me \and Someone Else}
\date{May 2013}
and then `
\maketitle
called within the document.
This will create the standard centred title.
If you want to change how the title etc is formatted you could use the titling package.
If you want to change how the section headers are formatted, you could use the titlesec package.
To automagically have pandoc implement these you could define your own template. A simpler option is to have a file with your desired latex preamble to be included in the header. and then use the appropriate arguments when calling pandoc (eg -H FILE or --include-in-header=FILE)

Resources