knitr Rnw latex: how to obtain a figure and caption in landscape mode that's full page width - knitr

I am trying to produce a full-page figure along with a caption in landscape mode. The Rnw file below works fine if I omit "fig.cap='Caption Trial'", but not if the caption is used. Any help would be greatly appreciated.
\documentclass{article}
\usepackage{fullpage}
\usepackage{pdflscape}
\begin{document}
\begin{landscape}
<<test, out.width='1\\linewidth', fig.width=7, fig.height=4, fig.cap='Caption Trial'>>=
par(mar=c(4, 4, .1, .1)); plot(1:10)
#
\end{landscape}
\end{document}

Try this:
\documentclass{article}
\usepackage{fullpage}
\usepackage{pdflscape}
\begin{document}
\begin{landscape}
\begin{figure}
<<test, out.width='1\\linewidth', fig.width=7, fig.height=4>>=
par(mar=c(4, 4, .1, .1)); plot(1:10)
#
\caption{Caption Trial}
\end{figure}
\end{landscape}
\end{document}

Related

Knitr option hook results in LaTeX output

I'm creating ioslides and beamer output from RMarkdown source files and need to have variable figure output dependent on the output format.
I've generate a plot using ggplot2 which renders fine.
I want the plot to have an out.width set to 100% for HTML output and 70% for LaTeX output.
The problem is that when I set the option hook and check for LaTeX output, the tex file generated by knitr contains the LaTeX source verbatim for including the image which renders as text in the slide.
## Modify the output based on the format we're exporting
knitr::opts_hooks$set (out.width = function (options) {
if (knitr::is_latex_output()) {
options$out.width = '70%'
}
options
})
The plot renders fine in HTML output.
However, for beamer I get as shown in the image:
And the resulting output in the .tex file:
\begin{frame}{Why bother?}
\protect\hypertarget{why-bother}{}
\textbackslash begin\{center\}\textbackslash includegraphics{[}width=70\% {]}\{slide-book\_files/figure-beamer/lec-4-modularity-cost-1\}
\textbackslash end\{center\}
\end{frame}
Here's complete code for a MWE:
## Modify the output based on the format we're exporting
knitr::opts_hooks$set (out.width = function (options) {
if (knitr::is_latex_output()) {
options$out.width = '70%'
}
return (options)
})
```{r, lec-4-modularity-cost, echo=FALSE, out.width='100%', fig.align='center'}
plot.data <- tibble (X = seq (0,100), Y = 2 * X)
ggplot(plot.data, aes(x=X, y=Y)) +
geom_path()
```
When setting out.width this way you have to use a format that LaTeX understands right away, i.e. 0.7\linewidth instead of 70%. And you have to double the backslash in the R code:
---
output:
html_document: default
pdf_document:
keep_tex: yes
---
```{r setup, include=FALSE}
## Modify the output based on the format we're exporting
knitr::opts_hooks$set (out.width = function (options) {
if (knitr::is_latex_output()) {
options$out.width = '0.7\\linewidth'
}
options
})
```
```{r, lec-4-modularity-cost, echo=FALSE, out.width='100%', fig.align='center'}
library(ggplot2)
library(tibble)
plot.data <- tibble (X = seq (0,100), Y = 2 * X)
ggplot(plot.data, aes(x=X, y=Y)) +
geom_path()
```

Pygal not rendering Legend properly in PNG

I am trying to render a chart in PNG format, because this image is going to be embed in a email template and SVG are not supported on every email client, when I render the chart in SVG format it looks quite fine but when I render on PNG, the chart is fine but the legend looks wicked
The code I am using is pretty straightforward. I've installed PyCairo tinycss and cssselect
...
pie_chart = pygal.Pie()
pie_chart.title = 'Email usage on %s ' % month_str
for k, v in data.items():
if k in EMAIL_STATUS:
pie_chart.add(k, float(v))
if settings.DEBUG == True:
path = os.path.join(settings.APP_ROOT, 'static')
else:
path = settings.STATIC_ROOT
path = '%s/images/chart.png' % path
pie_chart.render_to_png(path)
Any idea what am I missing here?
Thanks

How do you i use mandarin characters in matplotlib?

I have been trying to use matplotlib's text or annotate modules with mandarin Chinese characters. Somehow it ends up showing boxes. Any idea on this ?
Here is a solution that works for me on Python 2.7 and Python 3.3, using both text and annotate methods with Chinese.
import matplotlib.pyplot as plt
from matplotlib.font_manager import FontProperties
fig = plt.figure()
ax = fig.add_subplot(111)
ChineseFont1 = FontProperties(fname = 'C:\\Windows\\Fonts\\simsun.ttc')
ChineseFont2 = FontProperties('SimHei')
ax.text(3, 2, u'我中文是写得到的', fontproperties = ChineseFont1)
ax.text(5, 1, u'我中文是写得到的', fontproperties = ChineseFont2)
ax.annotate(u'我中文是写得到的', xy=(2, 1), xytext=(3, 4),
arrowprops=dict(facecolor='black', shrink=0.05),
fontproperties = ChineseFont1)
ax.axis([0, 10, 0, 10])
plt.show()
ChineseFont1 is hard coded to a font file, while ChineseFont2 grabs a font by family name (but for ChineseFont2 I had to try a couple to find one that would work). Both of those are particular to my system, in that they reference fonts I have, so you quite likely will need to change them to reference fonts/paths on your system.
The font loaded by default doesn't seem to support Chinese characters, so it was primarily a font choice issue.
Another solution is to use pgf backend which uses XeTeX. This allows one to use UTF-8 directly:
#!/usr/bin/env python2
# -*- coding:utf-8 -*-
import matplotlib
matplotlib.use("pgf")
pgf_with_custom_preamble = {
# "font.size": 18,
"pgf.rcfonts": False,
"text.usetex": True,
"pgf.preamble": [
# math setup:
r"\usepackage{unicode-math}",
# fonts setup:
r"\setmainfont{WenQuanYi Zen Hei}",
r"\setsansfont{WenQuanYi Zen Hei}",
r"\setmonofont{WenQuanYi Zen Hei Mono}",
],
}
matplotlib.rcParams.update(pgf_with_custom_preamble)
from matplotlib import pyplot as plt
x = range(5)
y = range(5)
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(x, y, label=u"我")
ax.legend(u"中")
ax.set_xlabel(u"是")
ax.set_ylabel(u"写")
ax.set_title(u"得")
ax.text(3, 2, u'到')
ax.annotate(u'的', xy=(2, 1), xytext=(3, 1),
arrowprops=dict(arrowstyle="<|-", connectionstyle="arc3", color='k'))
fig.savefig("pgf-mwe.png")
Result:
This solution requires matplotlib 1.2+ and probably XeTeX installed on Your system. The easiest way to get a working XeTeX is to go for any modern LaTeX distribution: TeXLive (available for all platforms) or MiKTeX (windows only).
matplotlib.rc('font', family='Source Han Sans CN')
ax = quarterly_gdp.plot(title='国内生产总值')
example
You only have to setup font family of your matplotlib and after that you can plot with Chinese labels. I've set up font to be Source Han Sans CN, as it's the only available font on my computer for Chinese.
You can check the available font by command fc-list :lang=zh.

How to add some text between source code and results exports to PDF?

Whenver I want to export a source code block with it's results, I want to add some text in-between, e.g. "Output:", because otherwise is very hard for the reader to understand where the program ends and the output begins - how do I achieve adding "Output:" in-between?
Example:
* Test
#+BEGIN_SRC python :results output :exports both
i = 5
print i
i = i + 1
print i
s = '''This is a multi-line string.
This is the second line.'''
print s
#+END_SRC
#+RESULTS:
: 5
: 6
: This is a multi-line string.
: This is the second line.
You need to name your source code block. Then, and only then, you can put your results part wherever you want, even before the source code block.
EDITED (add ECM):
#+name: my-block
#+begin_src emacs-lisp
(message "hello")
#+end_src
I can put paragraphs here...
#+results: my-block
: hello

How to align a paragraph (justify) whith Itext?

I have 2 lines and I want to align (justify) them.
I have this code:
Paragraph p=new Paragraph(ANC,fontFootData);
p.setLeading(1, 1);
p.setAlignment(Element.ALIGN_JUSTIFIED);
document.add(p);
Paragraph p2=new Paragraph(RUTTEL,fontFootData);
p2.setLeading(1, 1);
p2.setAlignment(Element.ALIGN_JUSTIFIED);
document.add(p2);
where ANC and RUTTEL are string, but they not be align.
Could anybody help me?
For a one line use ALIGN_JUSTIFIED_ALL, more than one line use ALIGN_JUSTIFIED.
If you are using for C#, then:
p.Alignment = Element.ALIGN_JUSTIFIED;