RMarkdown tikz engine in ioslides presentation - knitr

Whereas the following minimal example works with output: html_document it doesn't with output: ioslides_presentation.
Any ideas why?
---
title: "Tikz test"
output: ioslides_presentation
---
## Tikz Example
```{r,engine='tikz', fig.ext='svg'}
\begin{tikzpicture}
\draw (0,0) circle (2cm);
\end{tikzpicture}
```

Related

Package inputenc Error: Unicode character 하 (U+D558) (inputenc) not set up for use with LaTeX. (Automating Unicode Input)

I have read all the related answers around this issue and for most people using \DeclareUnicodeCharacter{}{} works for them. However, I'm generating my TEX file from an input file and I don't know the Unicode characters/words which I'm going to be given in advance, so it would be difficult to use the DeclareUnicodeCharacter declaration.
I'm using pdflatex which some StackExchange answers said wouldn't be as useful for Unicode characters, so I also tried xetex and luatex, but these didn't output as expected. Any suggestions how I could modify my script to output a tex file so that pdflatex will not barf on the Unicode characters? Or one which would be acceptable to xetex or luatex?
I've done simple pdflatex stuff but never used Unicode before.
I'm attempting to modify a script I found on github which generates bingo cards. The original program only dealt with English words in the "squares" input file.
I've added one line to the script in order to get it to see Unicode.
\usepackage[utf8]{inputenc}
the bash script:
#!/usr/bin/bash
## text file which contains bingo squares
input_text=squares
## latex code for center square
center_square='\includegraphics[width=0.9\textwidth]{center.png}'
#center_square=FREE
node=( AA AB AC AD AE BA BB BC BD BE CA CB CD CE DA DB DC DD DE EA EB EC ED EE )
cat > bingo.tex << EOF
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[a4paper,margin=2cm]{geometry}
\usepackage{xstring}
\usepackage[none]{hyphenat}
\usepackage{tikz}
\usetikzlibrary{calc}
\renewcommand{\familydefault}{\sfdefault}
\newcommand{\Size}{3cm}% Adjust size of square as desired
\def\NumOfColumns{5}%
\def\Sequence{1/A, 2/B, 3/C, 4/D, 5/E}% This needs to match \NumOfColumns
\tikzset{Square/.style={
inner sep=0pt,
text width=\Size,
minimum size=\Size,
draw=black,
align=center
}
}
EOF
i=0
OLDIFS=$IFS
IFS=$'\n'
for line in $(sort -R $input_text | head -n 24)
do
echo "\newcommand{\Node${node[i]}}{$line}%" >> bingo.tex
(( i++ ))
done
IFS=$OLDIFS
echo "\newcommand{\NodeCC}{$center_square}%" >> bingo.tex
cat >> bingo.tex << EOF
\pagestyle{empty}
\begin{document}
\centering
% Optional Title
\vspace*{\fill}
\begin{center}
\begin{tikzpicture}[draw=black, ultra thick, x=\Size,y=\Size]
\node at (0.5,-0.8) {\Huge B};
\node at (1.5,-0.8) {\Huge I};
\node at (2.5,-0.8) {\Huge N};
\node at (3.5,-0.8) {\Huge G};
\node at (4.5,-0.8) {\Huge O};
\foreach \col/\colLetter in \Sequence {%
\foreach \row/\rowLetter in \Sequence{%
\pgfmathtruncatemacro{\value}{\col+\NumOfColumns*(\row-1)}
\def\NodeText{\expandafter\csname Node\rowLetter\colLetter\endcsname}
\node [Square] at (\$(\col,-\row)-(0.5,0.5)\$) {\NodeText};
}
}
\end{tikzpicture}
\end{center}
\vspace*{\fill}
\end{document}
EOF
pdflatex bingo.tex
#luatex bingo.tex
#xetex bingo.tex
which generates the following bingo.tex file:
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[a4paper,margin=2cm]{geometry}
\usepackage{xstring}
\usepackage[none]{hyphenat}
\usepackage{tikz}
\usetikzlibrary{calc}
\renewcommand{\familydefault}{\sfdefault}
\newcommand{\Size}{3cm}% Adjust size of square as desired
\def\NumOfColumns{5}%
\def\Sequence{1/A, 2/B, 3/C, 4/D, 5/E}% This needs to match \NumOfColumns
\tikzset{Square/.style={
inner sep=0pt,
text width=\Size,
minimum size=\Size,
draw=black,
align=center
}
}
\newcommand{\NodeAA}{주다}%
\newcommand{\NodeAB}{되다}%
\newcommand{\NodeAC}{않다}%
\newcommand{\NodeAD}{것}%
\newcommand{\NodeAE}{보다}%
\newcommand{\NodeBA}{사람}%
\newcommand{\NodeBB}{거}%
\newcommand{\NodeBC}{없다}%
\newcommand{\NodeBD}{나}%
\newcommand{\NodeBE}{아니다}%
\newcommand{\NodeCA}{하다}%
\newcommand{\NodeCB}{그}%
\newcommand{\NodeCD}{수}%
\newcommand{\NodeCE}{이}%
\newcommand{\NodeDA}{있다}%
\newcommand{\NodeDB}{때}%
\newcommand{\NodeDC}{가다}%
\newcommand{\NodeDD}{우리}%
\newcommand{\NodeDE}{같다}%
\newcommand{\NodeCC}{\includegraphics[width=0.9\textwidth]{center.png}}%
\pagestyle{empty}
\begin{document}
\centering
% Optional Title
\vspace*{\fill}
\begin{center}
\begin{tikzpicture}[draw=black, ultra thick, x=\Size,y=\Size]
\node at (0.5,-0.8) {\Huge B};
\node at (1.5,-0.8) {\Huge I};
\node at (2.5,-0.8) {\Huge N};
\node at (3.5,-0.8) {\Huge G};
\node at (4.5,-0.8) {\Huge O};
\foreach \col/\colLetter in \Sequence {%
\foreach \row/\rowLetter in \Sequence{%
\pgfmathtruncatemacro{\value}{\col+\NumOfColumns*(\row-1)}
\def\NodeText{\expandafter\csname Node\rowLetter\colLetter\endcsname}
\node [Square] at ($(\col,-\row)-(0.5,0.5)$) {\NodeText};
}
}
\end{tikzpicture}
\end{center}
\vspace*{\fill}
\end{document}
You can see it output:
\newcommand{\NodeAA}{주다}%
as unicode to be input into the square, but pdflatex doesn't like it.
If you compile with lualatex or xelatex, you'll just have to find a font which has all your glyphs. In the example below, I'm using Arial Unicode MS.
To see which fonts you can use for your glyphes, you can use the command line tool albatross, see https://stackoverflow.com/a/69721465/2777074 for an example how to use it.
% !TeX TS-program = lualatex
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[a4paper,margin=2cm]{geometry}
\usepackage{xstring}
\usepackage[none]{hyphenat}
\usepackage{tikz}
\usetikzlibrary{calc}
\renewcommand{\familydefault}{\sfdefault}
\newcommand{\Size}{3cm}% Adjust size of square as desired
\def\NumOfColumns{5}%
\def\Sequence{1/A, 2/B, 3/C, 4/D, 5/E}% This needs to match \NumOfColumns
\tikzset{Square/.style={
inner sep=0pt,
text width=\Size,
minimum size=\Size,
draw=black,
align=center
}
}
\newcommand{\NodeAA}{주다}%
\newcommand{\NodeAB}{되다}%
\newcommand{\NodeAC}{않다}%
\newcommand{\NodeAD}{것}%
\newcommand{\NodeAE}{보다}%
\newcommand{\NodeBA}{사람}%
\newcommand{\NodeBB}{거}%
\newcommand{\NodeBC}{없다}%
\newcommand{\NodeBD}{나}%
\newcommand{\NodeBE}{아니다}%
\newcommand{\NodeCA}{하다}%
\newcommand{\NodeCB}{그}%
\newcommand{\NodeCD}{수}%
\newcommand{\NodeCE}{이}%
\newcommand{\NodeDA}{있다}%
\newcommand{\NodeDB}{때}%
\newcommand{\NodeDC}{가다}%
\newcommand{\NodeDD}{우리}%
\newcommand{\NodeDE}{같다}%
\newcommand{\NodeCC}{\includegraphics[width=0.9\textwidth]{example-image-duck}}%
\pagestyle{empty}
\usepackage{fontspec}
\setsansfont{Arial Unicode MS}
\begin{document}
\centering
% Optional Title
\vspace*{\fill}
\begin{center}
\begin{tikzpicture}[draw=black, ultra thick, x=\Size,y=\Size]
\node at (0.5,-0.8) {\Huge B};
\node at (1.5,-0.8) {\Huge I};
\node at (2.5,-0.8) {\Huge N};
\node at (3.5,-0.8) {\Huge G};
\node at (4.5,-0.8) {\Huge O};
\foreach \col/\colLetter in \Sequence {%
\foreach \row/\rowLetter in \Sequence{%
\pgfmathtruncatemacro{\value}{\col+\NumOfColumns*(\row-1)}
\def\NodeText{\expandafter\csname Node\rowLetter\colLetter\endcsname}
\node [Square] at ($(\col,-\row)-(0.5,0.5)$) {\NodeText};
}
}
\end{tikzpicture}
\end{center}
\vspace*{\fill}
\end{document}

Drawing using Tikz

I would like to include some shapes using tikz code in my latex thesis. This is what i have so far drawn.
\begin{document}
\begin{tikzpicture}
\node[isosceles triangle,
draw,
rotate=90,
minimum size =2cm] (T1)at (0,0){};
\end{tikzpicture}
\begin{tikzpicture}
\node[single arrow, draw=blue, very thick,
minimum width = 40pt, single arrow head extend=3pt,
minimum height=26mm,
rotate=90] {}; % length of arrow
\end{tikzpicture}
\end{document}
I need something similar to this:
But replacing the BH in the top circles with CO.
Something to get you started. I highly recommend to take a look a the TikZ manual.
\documentclass[tikz, border=1mm]{standalone}
\usetikzlibrary{hobby}
\begin{document}
\begin{tikzpicture}[scale=2]
\draw[thick, ->] (0,.5) -- (10,.5)
node[pos=.25, yshift=3mm] {$0.01$}
node[pos=.5, yshift=3mm] {$0.1$}
node[pos=.75, yshift=3mm] {$1$}
node[pos=1, anchor=east, yshift=3mm] {$\dot{M}/\dot{M}_{Edd}$};
\draw[fill=lightgray] (0,1) -- (1.25,3) -- (2.5,1) -- cycle;
\node[rotate=-90] at (1.25,1.75) {ADAF};
\draw[top color=lightgray, bottom color=black] (4,1) -- (4.1,2) .. controls (4.1,2.15) and (3.6,2) .. (3.75,2.25) -- (4.2,3) -- (4.65,2.25) .. controls (4.8,2) and (4.3,2.15) .. (4.3,2) -- (4.4,1) -- cycle;
\node[rotate=-90] at (4.2,2.45) {ADAF};
\node[rotate=-90, white] at (4.2,1.45) {Standard};
\draw[fill] (5,1) -- (5.2,2.25) -- (5.4,1) -- cycle;
\draw (5.2,2.125) -- ++(.5,.25) node[anchor=west, align=center] {unstable \\ Standard};
\draw[fill] (9,1) -- (9.1,2) .. controls (9.1,2.15) and (8.325,2) .. (8.55,2.25) -- (9.2,3) -- (9.85,2.25) .. controls (10.075,2) and (9.3,2.15) .. (9.3,2) -- (9.4,1) -- cycle;
\node[rotate=-90, white] at (9.2,2.45) {Slim};
\node[rotate=-90, white] at (9.2,1.45) {Standard};
\node[circle, white, fill=black] at (1.25,3.35) {CO};
\node[circle, white, fill=black] at (4.2,3.35) {CO};
\node[circle, white, fill=black] at (5.2,3.35) {CO};
\node[circle, white, fill=black] at (9.2,3.35) {CO};
\end{tikzpicture}
\end{document}

Using tikz graphdrawing library within RMarkdown ... Need to use lualatex engine, but can't get it to work

I have the following code in an rmd file which leverages tikz for diagrams:
---
title: "TestNonTufteLua"
author: "Me"
output:
pdf_document :
latex_engine: lualatex
---
Prove tikz works:
```{r tikTest1, engine = "tikz"}
\usetikzlibrary{shapes}
\begin{tikzpicture}
\node[ellipse, draw=black, align = center] (Data) {Data $y_{n}$};
\end{tikzpicture}
```
Then, when you set `eval = TRUE` in the below code, it will not work.
```{r tikTest2, eval = FALSE, engine = "tikz"}
\usetikzlibrary{graphs, graphdrawing}
\usegdlibrary{layered}
\tikz [gr/.style={gray!50}, font=\bfseries]
\graph [layered layout] {
% A and F are horizontally aligned if you also set weight=0.5 for A -- C.
A -- [minimum layers=2] C -- F,
{ [nodes=gr, edges=gr] A -- B -- { E, D -- F } }
};
```
When changing to eval=TRUE in the second chunk, I get the following
error:
Quitting from lines 24-29 (testNonTufteLua.Rmd) Error: running
'texi2dvi' on '.\tikz36747a021b22.tex' failed
LaTeX errors: rarygraphdrawing.code.tex:22: Package pgf Error: You
need to run LuaTeX to use the graph drawing library.
This error occurs when using the knit button from RStudio or using render("testNonTufteLua.Rmd", output_format = pdf_document(keep_tex = TRUE, latex_engine = "lualatex"). I have also experimented with setting options(tikzDefaultEngine = "luatex") to get tikzDevice to handle it properly, but it still does not work. I just can't seem to get the graphdrawing library to work even though the tikz-shapes library can be loaded and also that the rest of the document seems to be compiled with lualatex. Thanks for any help!!
Update: Meanwhile knitr no longer uses tools::texi2dvi but tinytex::latexmk. One therefore has to use options(tinytex.engine = 'lualatex') in a set-up chunk.
This is rather tricky, since you are not using tikzDevice but the tikz engine, which uses tools::texi2dvi to convert to PDF. You can change this using options(texi2dvi = "lualatex"). However, the default template does not work with LuaLaTeX. I have therefore created a modified template:
\RequirePackage{luatex85}
\documentclass{article}
\usepackage[luatex,active,tightpage]{preview}
\usepackage{amsmath}
\usepackage{tikz}
\usetikzlibrary{matrix}
\begin{document}
\begin{preview}
%% TIKZ_CODE %%
\end{preview}
\end{document}
And specify that file with engine.opts = list(template = "tikz2pdf.tex"). Putting it all together here my working file:
---
title: "TestNonTufteLua"
author: "Me"
output:
pdf_document :
latex_engine: lualatex
---
```{r}
options(texi2dvi = "lualatex")
```
```{r tikTest2, eval = TRUE, engine = "tikz", engine.opts = list(template = "tikz2pdf.tex")}
\usetikzlibrary{graphs, graphdrawing}
\usegdlibrary{layered}
\tikz [gr/.style={gray!50}, font=\bfseries]
\graph [layered layout] {
% A and F are horizontally aligned if you also set weight=0.5 for A -- C.
A -- [minimum layers=2] C -- F,
{ [nodes=gr, edges=gr] A -- B -- { E, D -- F } }
};
```
Result:
References:
how to set engine options
preview and LuaLaTeX
knitr using texi2pdf
A small running variation of the example above is the following using tinytex.
---
title: "lualatex. Using `tinytex.engine`"
output:
html_document:
df_print: paged
pdf_document:
latex_engine: lualatex
---
## Latex engines
By default, PDF documents are rendered using `pdflatex`. You can specify an
alternate engine using the `latex_engine` option. Available engines
are `pdflatex`, `xelatex`, and `lualatex.`
```{r setup}
options(tinytex.engine = "lualatex")
```
```{r tikzLua, eval = TRUE, engine = "tikz", engine.opts = list(template = "tikz2pdf.tex")}
\usetikzlibrary{graphs, graphdrawing}
\usegdlibrary{layered}
\tikz [gr/.style={gray!50}, font=\bfseries]
\graph [layered layout] {
% A and F are horizontally aligned if you also set weight=0.5 for A -- C.
A -- [minimum layers=2] C -- F,
{ [nodes=gr, edges=gr] A -- B -- { E, D -- F } }
};
```
After an update in knitr the example above stopped running.

Caption not displaying table

When I use kable to create a table in my Rnw file, the table outputs as expected. However when I include the caption, the table ceases to output at all. Below is my latex and sample knitr chunk.
\documentclass[titlepage]{article}
\usepackage{graphicx}
\usepackage[dvipsnames]{xcolor}
\usepackage{afterpage}
\usepackage{booktabs}
\usepackage{longtable}
\pagecolor{ForestGreen}\afterpage{\nopagecolor}
\color{white}
\title{%
\noindent\makebox[\linewidth]{\rule{9cm}{0.9pt}}
\huge \textbf{\textit{Key Distributor Report}}\\
\LARGE \vspace{2mm} \textbf{\textit{A detailed quarterly review}} \\
\noindent\makebox[\linewidth]{\rule{9cm}{0.9pt}}
\vspace{6mm} \LARGE Macdonald Corp.}
\pagenumbering{arabic}
\begin{document}
\maketitle
\color{black}
\newpage
\tableofcontents
\newpage
\section{Overview of Key Distributors}
\section{Company A}
\subsection{National Overview}
\subsubsection{Account base structure}
<<
tester, echo=FALSE, message=FALSE, warning=FALSE, results='asis',fig.height=3, fig.width=3, fig.align='center', eval=TRUE >>=
library(knitr)
kable(head(mtcars[,1:3]), booktabs=TRUE, caption='This is a caption')
#
\end{document}
This seems to be a problem of coloring the texts. If you try to select all content in the pdf you realize, that the table is actually there, but the text is white. So simply change the way of coloring the title page (use \color on \maketitle instead of \title):
\documentclass[titlepage]{article}
\usepackage{graphicx}
\usepackage[dvipsnames]{xcolor}
\usepackage{afterpage}
\usepackage{booktabs}
\usepackage{longtable}
\pagecolor{ForestGreen}\afterpage{\nopagecolor}
\title{%
\noindent\makebox[\linewidth]{\rule{9cm}{0.9pt}}
\huge \textbf{\textit{Key Distributor Report}}\\
\LARGE \vspace{2mm} \textbf{\textit{A detailed quarterly review}} \\
\noindent\makebox[\linewidth]{\rule{9cm}{0.9pt}}
\vspace{6mm} \LARGE Macdonald Corp.}
\pagenumbering{arabic}
\begin{document}
{\color{white}%
\maketitle
}
\color{black}
\newpage
\tableofcontents
\newpage
\section{Overview of Key Distributors}
\section{Company A}
\subsection{National Overview}
\subsubsection{Account base structure}
<<tester, echo=FALSE, message=FALSE, warning=FALSE,results='asis',fig.height=3, fig.width=3, fig.align='center', eval=TRUE >>=
library(knitr)
library(kableExtra)
library(dplyr)
kable(head(mtcars[,1:3]), booktabs=T, caption='This is a caption') %>%
kable_styling(latex_options = "hold_position")
#
\end{document}

How to increase the font size of one node label in tikz

I have a tikz picture:
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning, shapes, shadows, arrows}
\begin{document}
\thispagestyle{empty}
\tikzstyle{abstract}=[circle, draw=black, fill=white]
\tikzstyle{labelnode}=[circle, draw=white, fill=white]
\tikzstyle{line} = [draw, -latex']
\begin{tikzpicture}[
every node/.style={line width=2mm, circle split, draw, minimum size=5cm}
]
\node (output) [thick, font=\fontsize{60}{60}\selectfont, thick] {$y_{(out)}$ \nodepart{lower} $y_{(in)}$};
\node (hidden) [thick, font=\fontsize{60}{60}\selectfont, below=1cm of output] {$h_{(out)}$ \nodepart{lower} $h_{(in)}$};
\node (input) [thick, font=\fontsize{60}{60}\selectfont, below=1cm of output, abstract, below=of hidden] {$x$};
\draw[line width=1mm, ->] (input) -- (hidden) node[font=\fontsize{60}{60}\selectfont, below=of output, labelnode, midway, right=2cm] {$W_1\, {\rm{, }} \,b_1$};
\draw[line width=1mm, ->] (hidden) -- (output) node[font=\fontsize{60}{60}\selectfont, below=of output, labelnode, midway, right=2cm] {$W_2 \, {\rm{, }} \,b_2$};
\end{tikzpicture}
\end{document}
I would like to increase the font size of "x" in the bottom node. For some reason, altering the values in font=\fontsize{60}{60} does nothing (increasing or decreasing made no difference in the size). Any idea how I can make the x take up more area inside the node?
I found the answer here, and used the lmodern package.
\documentclass{standalone}
\usepackage{tikz}
\usepackage{lmodern}
\usetikzlibrary{positioning, shapes, shadows, arrows}
\begin{document}
\thispagestyle{empty}
\tikzstyle{abstract}=[circle, draw=black, fill=white]
\tikzstyle{labelnode}=[circle, draw=white, fill=white]
\tikzstyle{line} = [draw, -latex']
\begin{tikzpicture}[
every node/.style={line width=2mm, circle split, draw, minimum size=5cm}
]
\node (output) [thick, font=\fontsize{60}{0}\selectfont, thick] {$y_{(out)}$ \nodepart{lower} $y_{(in)}$};
\node (hidden) [thick, font=\fontsize{60}{0}\selectfont, below=1cm of output] {$h_{(out)}$ \nodepart{lower} $h_{(in)}$};
\node (input) [thick, font=\fontsize{80}{0}\selectfont, below=1cm of output, abstract, below=of hidden] {$x$};
\draw[line width=1mm, ->] (input) -- (hidden) node[font=\fontsize{60}{0}\selectfont, below=of output, labelnode, midway, right=3cm] {$W_1\, {\rm{, }} \,b_1$};
\draw[line width=1mm, ->] (hidden) -- (output) node[font=\fontsize{60}{0}\selectfont, below=of output, labelnode, midway, right=3cm] {$W_2 \, {\rm{, }} \,b_2$};
\end{tikzpicture}
\end{document}