Is there a way to customize margins in yaml rticles, R [duplicate] - r-markdown

I have created an RMarkdown file in RStudio and managed to knit it with knitr into an HTML and .md file. Next, I used pandoc to convert the .md file into a PDF file (I get an error if I try and convert from the .html file). However, the PDF that is produced have massive margins (like this http://johnmacfarlane.net/pandoc/demo/example13.pdf). How can I get pandoc to produce something with smaller margins? I have looked through the pandoc user guide, but haven't found anything useful.

Recent versions of rmarkdown and pandoc
In more recent versions of rmarkdown, the settings of margins can be done in the YAML header via the top-level element geometry. What you specify in the geometry tag will be piped into the LaTeX template that ships with Pandoc via the following LaTeX snippet
$if(geometry)$
\usepackage[$for(geometry)$$geometry$$sep$,$endfor$]{geometry}
$endif$
For example, to specify margins that are 2cm in width one would include
---
title: "Habits"
author: John Doe
date: March 22, 2005
geometry: margin=2cm
output: pdf_document
---
For more complex specifications to be passed to the geometry LaTeX package, string options together as you would with LaTeX:
---
title: "Habits"
author: John Doe
date: March 22, 2005
geometry: "left=3cm,right=3cm,top=2cm,bottom=2cm"
output: pdf_document
---
Original answer
This is a LaTeX question as Pandoc is rendering to PDF via LaTeX - what you linked to represents the default margins on a LaTeX document.
The geometry LaTeX package for example can be used to alter the margins of the page. However you'll need a way to tell Pandoc to use this by including it ins the LaTeX header applied to the converted md file.
How you do this is documented in the Pandoc User Guide. See in particular the --template=FILE command line argument and the Templates section. Essentially, either find and modify the default template to include the LaTeX instructions you want to use or start your own template from scratch and place it in the appropriate location; see the --data-dir command line argument.
Another alternative if you are using a recent version of Pandoc is to use the variable argument (set either with -V KEY[=VAL] or --variable=KEY[:VAL]). The geometry package was added to the default LaTeX template in May 2012 (see this discussion). As such, if you wanted to change the page margins, you can use:
pandoc -V geometry:margin=1in -o output.pdf input.md
You can specify multiple variable values too. For instance, if you wanted to create a 4 by 6 inch pdf with half-inch margins, you can use:
pandoc -V geometry:paperwidth=4in -V geometry:paperheight=6in -V geometry:margin=.5in -o output.pdf input.md

In more recent versions of pandoc, you can set a number of parameters in the YAML header. You can set the geometry here, for example:
---
title: Pandoc nice margins example
author: naught101
geometry: margin=3cm
---
body text of document
When pandoc converts it to a pdf, it should have correct margins.

FTR: "recent version" above seems to include Knitr 1.30, before I needed following header:
---
pdf_document: null
geometry: margin=1cm
output: pdf_document
---
Without the additional pdf_document: null the geometry setting had no effect. With it, settings from https://bookdown.org/yihui/rmarkdown-cookbook/latex-variables.html worked.

Related

How to indent the first line in each paragraph in Quarto docx output?

I am currently writing a report that I will export to a docx file. I would like to have an indentation at the beginning of each paragraph but I couldn't find how to do so in Quarto in Rstudio.
Thanks
For Word documents specifically, create a reference document with the Quarto console command
quarto pandoc -o custom-reference-doc.docx \
--print-default-data-file reference.docx
Set up the indentation style within Word for your reference .docx file. Use that file for styling your Quarto doc with YAML
format:
docx:
reference-doc: custom-reference-doc.docx
More info here https://quarto.org/docs/output-formats/ms-word-templates.html

doxygen: Table of contents broken in the pdf

After running Doxygen on my C++ code, I built the pdf documentation like this :
pdflatex refman.tex
All page numbers in the table of contents in the resulting pdf file are ??. What could be the reason and how can it be fixed ?
UPDATE: Running pdflatex a second time fixes the table of contents...
It's not due to Doxygen but the way you generated the pdf with pdflatex.
You just have to recompile a second time with pdflatex to get the expected result.

RTF syntax highlighting in Doxygen

I'm trying to create an RTF file to use in MS word of all the C++ files in my Visual Studio project. I'm using the most recent Doxygen and I set SOURCE_BROWSER to true, GENERATE_RTF to true, and RTF_SOURCE_CODE to true.
It says it will provide source code with syntax highlighting, but it just pastes all the code in Courier New font without any form of styling whatsoever. How can I get it to provide C++ syntax highlighting in its RTF output?
Indeed although stated in the documentation / Doxyfile the syntax highlighting was not implemented in doxygen.
I've just pushed a proposed patch to github (pull request 741, https://github.com/doxygen/doxygen/pull/741) that implements the syntax highlighting for RTF.
2018/06/10: The push request has been integrated in the master version on github.

Google c style settings for gnu indent?

I am using google c indent style for Emacs (google-c-style.el) and Vim(google.vim).
But since I have some existing code that is not this style and I hope I can change it. I find there is a tool called GNU indent that can do such thing automatically and it provides some common style settings on this page, however there is no for Google c indent style. SO is there equivalent for it as well?
(I tried the Linux and Berkley style and feel that they are by no means satisfactory for me)
For the record, there is an alternate solution for those who are interested in Clang and LLVM.
clang-format definitely can help format existing source code easily and efficiently. It has explicit builtin support for 5 format, namely LLVM(default), Google, Chromium, Mozilla, WebKit.
The simples way to format a file with Google style is:
clang-format -style=Google -i filename
Where -i means inplace modification, you may try without this option to preview the changes.
To batch format existing C/C++ code we can simply use the command like:
find . -name "*.cc" | xargs clang-format -style=Google -i
Apart from the listed 5 formats, there are actually other styles like GNU(added on revision 197138; it's a pity that the document is not synced).
Note that clang-format accepts rc like files named .clang-format or _clang-format in a project, the simplest way to add such a configuration file(as said in clang-format's official tutorial page) is to dump the configuration of an existing format like:
clang-format -style=Google -dump-config >.clang-format
Also you might also use BasedOnStyle option so a configuration file might look like:
---
BasedOnStyle: Chromium
PointerBindsToType: false
ObjCSpaceAfterProperty: true
...
Use .clang-format or _clang-format as keywords to search on Github and there are other samples; or you might refer to this site to help build one.
There are also integrations for IDEs/Editors such as Visual Studio(in directory clang-format-vs), Sublime, Emacs, Vim(all in directory clang-format).
Another 3 tips:
For Emacs integration(clang-format.el), personally I think it's better to bind key for clang-format-buffer rather than clang-format-region.
For Mac OSX homebrew installation, use brew install --with-clang, --with-lld, --with-python --HEAD llvm can get clang-format support and its integration files are in $(brew --cache)/llvm--clang--svn-HEAD/tools/clang-format(bonus: there is even a git-clang-format there!!).
There are other fabulous tools inside clang-extra-tools such as clang-modernize(which is used to "automatically convert C++ code written against old standards to use features of the newest C++ standard where appropriate"), really worthy of having a try!
A brief reading of the google coding style shows that it is mostly K&R coding style, except with 2 space indentation (including case statements), 80 column lines, and no tabs. So, the following options should accomplish that:
-kr -ci2 -cli2 -i2 -l80 -nut
Start with that. You may have to tweak the resulting code. C++ support, in particular, is weak for indent.
Legend:
-kr: K&R style
-ci2: Continuation indent, the lines following the first line of a multi-line code statement get indented by 2 spaces
-cli2: Case label indent, case labels are indented 2 spaces in from the switch
-i2: Indentation, 2 spaces
-l80: Length, 80 columns
-nut: No tabs
As an alternative, you may consider executing emacs in batch mode to apply indentation on your code for you. Briefly:
Create a file called emacs-format-file with the contents:
(defun emacs-format-function ()
"Format the whole buffer."
(c-set-style "Google")
(indent-region (point-min) (point-max) nil)
(untabify (point-min) (point-max))
(save-buffer))
Execute the following command from the shell:
emacs -batch your_source_file.c \
-l emacs-format-file -f emacs-format-function

How to convert R Markdown to PDF?

I've previously asked about the commands for converting R Markdown to HTML.
What is a good way to convert R Markdown files to PDF documents?
A good solution would preserve as much as possible of the content (e.g., images, equations, html tables, etc.). The solution needs to be able to be run from the command-line. A good solution would also be cross-platform, and ideally minimise dependencies to make it easier to share makefiles and so forth.
Specifically, there are a lot of options:
Whether to convert RMD to MD to HTML to PDF; or RMD to MD to PDF; or RMD to PDF
If using the markdown package in R, which options to specify
Whether to use pandoc, a package built into R, or something else
Here's an example rmd file that presumably provides a reasonable test of any proposed solution. It was used as the basis for this blog post.
Updated Answer (10 Feb 2013)
rmarkdown package:
There is now an rmarkdown package available on github that interfaces with Pandoc.
It includes a render function. The documentation makes it pretty clear how to convert rmarkdown to pdf among a range of other formats. This includes including output formats in the rmarkdown file or running supplying an output format to the rend function. E.g.,
render("input.Rmd", "pdf_document")
Command-line:
When I run render from the command-line (e.g., using a makefile), I sometimes have issues with pandoc not being found. Presumably, it is not on the search path.
The following answer explains how to add pandoc to the R environment.
So for example, on my computer running OSX, where I have a copy of pandoc through RStudio, I can use the following:
Rscript -e "Sys.setenv(RSTUDIO_PANDOC='/Applications/RStudio.app/Contents/MacOS/pandoc');library(rmarkdown); library(utils); render('input.Rmd', 'pdf_document')"
Old Answer (circa 2012)
So, a number of people have suggested that Pandoc is the way to go. See notes below about the importance of having an up-to-date version of Pandoc.
Using Pandoc
I used the following command to convert R Markdown to HTML (i.e., a variant of this makefile), where RMDFILE is the name of the R Markdown file without the .rmd component (it also assumes that the extension is .rmd and not .Rmd).
RMDFILE=example-r-markdown
Rscript -e "require(knitr); require(markdown); knit('$RMDFILE.rmd', '$RMDFILE.md'); markdownToHTML('$RMDFILE.md', '$RMDFILE.html', options=c('use_xhml'))"
and then this command to convert to pdf
Pandoc -s example-r-markdown.html -o example-r-markdown.pdf
A few notes about this:
I removed the reference in the example file which exports plots to imgur to host images.
I removed a reference to an image that was hosted on imgur. Figures appear to need to be local.
The options in the markdownToHTML function meant that image references are to files and not to data stored in the HTML file (i.e., I removed 'base64_images' from the option list).
The resulting output looked like this. It has clearly made a very LaTeX style document in contrast to what I get if I print the HTML file to pdf from a browser.
Getting up-to-date version of Pandoc
As mentioned by #daroczig, it's important to have an up-to-date version of Pandoc in order to output pdfs. On Ubuntu as of 15th June 2012, I was stuck with version 1.8.1 of Pandoc in the package manager, but it seems from the change log that for pdf support you need at least version 1.9+ of Pandoc.
Thus, I installed caball-install.
And then ran:
cabal update
cabal install pandoc
Pandoc was installed in ~/.cabal/bin/pandoc
Thus, when I ran pandoc it was still seeing the old version.
See here for adding to the path.
I think you really need pandoc, which great software was designed and built just for this task :) Besides pdf, you could convert your md file to e.g. docx or odt among others.
Well, installing an up-to-date version of Pandoc might be challanging on Linux (as you would need the entire haskell-platform˙to build from the sources), but really easy on Windows/Mac with only a few megabytes of download.
If you have the brewed/knitted markdown file you can just call pandoc in e.g bash or with the system function within R. A POC demo of that latter is implemented in the Ṗandoc.convert function of my little package (which you must be terribly bored of as I try to point your attention there at every opportunity).
Right now (August 2014) You could use RStudio for converting R Markdown to PDF.
Basically, RStudio use pandoc to convert Rmd to PDF.
You could change metadata to:
Add table of contents
Change figure options
Change syntax highlighting style
Add LaTeX options
And many more...
For more details - http://rmarkdown.rstudio.com/pdf_document_format.html
For an option that looks more like what you get when you print from a browser, wkhtmltopdf provides one option.
On Ubuntu
sudo apt-get install wkhtmltopdf
And then the same command as for the pandoc example to get to the HTML:
RMDFILE=example-r-markdown
Rscript -e "require(knitr); require(markdown); knit('$RMDFILE.rmd', '$RMDFILE.md'); markdownToHTML('$RMDFILE.md', '$RMDFILE.html', options=c('use_xhml'))"
and then
wkhtmltopdf example-r-markdown.html example-r-markdown.pdf
The resulting file looked like this. It did not seem to handle the MathJax (this issue is discussed here), and the page breaks are ugly. However, in some cases, such a style might be preferred over a more LaTeX style presentation.
Only two steps:
Install the latest release "pandoc" from here:
https://github.com/jgm/pandoc/releases
Call the function pandoc in the library(knitr)
library(knitr)
pandoc('input.md', format = 'latex')
Thus, you can convert your "input.md" into "input.pdf".
I found using R studio the easiest way, but if wanting to control from the command line, then a simple R script can do the trick using rmarkdown render command (as mentioned above). Full script details here
#!/usr/bin/env R
# Render R markdown to PDF.
# Invoke with:
# > R -q -f make.R --args my_report.Rmd
# load packages
require(rmarkdown)
# require a parameter naming file to render
if (length(args) == 0) {
stop("Error: missing file operand", call. = TRUE)
} else {
# read report to render from command line
for (rmd in commandArgs(trailingOnly = TRUE)) {
# render Rmd to PDF
if ( grepl("\\.Rmd$", rmd) && file.exists(rmd)) {
render(rmd, pdf_document())
} else {
print(paste("Ignoring: ", rmd))
}
}
}
If you don't want to install anything you can output html. Then open the html file - it should open in a browser window, then right click to print. In the print window, select "save as pdf" in the bottom right hand corner if you're on a Mac. Voila!
Follow these simple steps :
1: In the Rmarkdown script run Knit(Ctrl+Shift+K)
2: Then after the html markdown is opened click Open in Browser(top left side) and
the html is opened in your web browser
3: Then use Ctrl+P and save as PDF .