R DiagrammeR Flush Subscript and Superscript within Labels - r

Long time reader, first time poster.
I am attempting to write mechner notation using DiagrammeR (through R Studio; everything is up to date; MacOS). Long term intention is to embed the code within a latex doc, but I'm taking this one step at a time. Currently, I cannot find the code necessary to force a subscript and superscript to be in-line with each other. So far, I have found this:
library(DiagrammeR)
grViz("
digraph dot {
graph [layout = dot, rankdir = LR]
node [fontname = Helvetica, shape = plaintext]
a [label = 'aA']
b [label = 'bA#_{2}#^{p}']
c [label = 'abC']
a -> b -> c
}")
Produces this.
But I need this.
I suppose the real question is how to properly format within those pesky quotations. I have yet to find a walkthrough on how to properly format (e.g., bold, italics, superscript) within the label field quotations. Suggestions?

I figured this out and forgot to post an answer! Here is the code that worked for me (Mac OSx; R Studio Sweave File, w/ pdfLaTeX enabled):
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix}
\newcommand{\1}{\textbf} %to save space
\begin{document}
\SweaveOpts{concordance=TRUE}
%example 1
\par{\centering
\begin{tikzpicture}
\matrix (m) [matrix of math nodes, nodes in empty cells, row
sep=1em,column sep=2em,minimum width=4em,nodes={anchor=center}]
{
a\1{A} & b\1{A}_{2}^{p} & ab\1{C} & \\
};
\draw [->] (m-1-1) -- (m-1-2);
\draw [->] (m-1-2) -- (m-1-3);
\end{tikzpicture}
\par}
%example 2
\par{\centering
\begin{tikzpicture}
\matrix (m) [matrix of math nodes, nodes in empty cells, row
sep=1em,column sep=2em,minimum width=4em,nodes={anchor=center}]
{
t\1{S}_{A}^{D} & s\1{R}_{CR} & t\1{S}_{CR} & \\
};
\draw [->] (m-1-1) -- (m-1-2);
\draw [->] (m-1-2) -- (m-1-3);
\end{tikzpicture}
\par}
\end{document}
Here is the output

Related

How to output LaTeX/TeX Tikz packages inside Julia

I wanted to output Circuitikz inside my Julia Notebook, How do I install circuitikz to my notebook, Or how do I run Circuitikz inside my Julia code
using Latexify
using LaTeXStrings
str = L"""
\begin{center}
\begin{circuitikz}
\tikz \draw (0,0) to[R=5<\sigma>] (2,0) to [V=5<\volt>] ( 3,0)
;
\end{circuitikz}
\caption{My first circuit. Just for learning Circuitikz}
\end{center}
"""
latexify(str)
Output
"$\begin{center}
\begin{circuitikz}
\tikz \draw (0,0) to[R=5<\sigma>] (2,0) to [V=5<\volt>] ( 3,0)
;
\end{circuitikz}
\caption{My first circuit. Just for learning LaTeX}
\end{center}
$"

TikZ: shaded area enveloping a graph

I need to draw a shaded area enveloping a graph like in the example below. I am looking for an automated procedure and a more satisfying result. Compared to the example, I need smoother edges and more regular distances between vertices (circles) and the boundaries of the area.
Drawing arbitrary shapes in TikZ are time consuming, I suspect that a smart trick might do the job, like creating large overlapping areas centered at each vertex. Any suggestions?
And here is a minimal working LaTeX code to generate the graph which has to be enveloped:
\documentclass[tikz]{standalone}
\usepackage{tikz}
\begin{document}
\tikzstyle{vertex}=[circle,draw=black,thick]
\begin{tikzpicture}
\foreach \pos/\name in {{(0,1)/1}, {(1,1)/2}, {(0,0)/3}, {(1,0)/4}, {(2,0)/5}, {(1,-.7)/6}}
\node[vertex] (\name) at \pos {};
\foreach \one/\two in {{1/2}, {1/3}, {1/4}, {1/6}, {2/3}, {2/4}, {2/5}, {3/6}, {4/6}, {4/5}}
\draw[thick] (\one) -- (\two);
\end{tikzpicture}
\end{document}
You could connect your nodes with a polygon with a tick stroke:
\documentclass[border=2mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{backgrounds}
\begin{document}
\tikzset{vertex/.style={circle,draw=black,thick}}
\begin{tikzpicture}
\foreach \pos/\name in {{(0,1)/1}, {(1,1)/2}, {(0,0)/3}, {(1,0)/4}, {(2,0)/5}, {(1,-.7)/6}}
\node[vertex] (\name) at \pos {};
\foreach \one/\two in {{1/2}, {1/3}, {1/4}, {1/6}, {2/3}, {2/4}, {2/5}, {3/6}, {4/6}, {4/5}}
\draw[thick] (\one) -- (\two);
\begin{scope}[on background layer]
\filldraw[lightgray,line width=20pt,rounded corners=5pt] (1.center) -- (2.center) -- (5.center) -- (4.center) -- (6.center) -- (3.center) -- cycle;
\end{scope}
\end{tikzpicture}
\end{document}

Figures with boxes, arrows etc. in Markdown

I am interested in displaying a causal mechanism visually with some plain boxes/circles connected by arrows in my paper written in RMarkdown. Something like this:
How is this done in RMarkdown? Would you recommend me to create a picture in a another app and import to RMarkdown like a picture?
I think the best way to do this is to use Tikz to draw the graphics. There's an example at https://github.com/yihui/knitr-examples/blob/master/058-engine-tikz.Rmd, where the code chunk includes
```{tikz, tikz-ex, fig.cap = "Funky tikz", fig.ext = 'png', cache=TRUE}
\usetikzlibrary{arrows}
\begin{tikzpicture}[node distance=2cm, auto,>=latex', thick, scale = 0.5]
\node (P) {$P$};
\node (B) [right of=P] {$B$};
\node (A) [below of=P] {$A$};
\node (C) [below of=B] {$C$};
\node (P1) [node distance=1.4cm, left of=P, above of=P] {$\hat{P}$};
\draw[->] (P) to node {$f$} (B);
\draw[->] (P) to node [swap] {$g$} (A);
\draw[->] (A) to node [swap] {$f$} (C);
\draw[->] (B) to node {$g$} (C);
\draw[->, bend right] (P1) to node [swap] {$\hat{g}$} (A);
\draw[->, bend left] (P1) to node {$\hat{f}$} (B);
\draw[->, dashed] (P1) to node {$k$} (P);
\end{tikzpicture}
```
and the output (in HTML or PDF format; this is from PDF) looks like this:

How to make a graph (nodes and edges) on Latex?

I new coding in latex and I want to make high-quality graphs (nodes and edges), in particular, I need to make this kind of plots.
Is it that even possible?
That is not only "even possible", that is always possible. This sort of diagram can be easily created using TikZ and similar packages.
If you already have the coordinates of the nodes, the directions of the arrows etc., you may start from this. This is not your diagram, but is a good starting point.
\documentclass[tikz,margin=3]{standalone}
\usetikzlibrary{shadows} % Shadows for nodes
\begin{document}
\begin{tikzpicture}
\tikzset{% This is the style settings for nodes
dep/.style={circle,minimum size=1cm,fill=orange!20,draw=orange,
general shadow={fill=gray!60,shadow xshift=1pt,shadow yshift=-1pt}},
cli/.style={circle,minimum size=1cm,fill=white,draw,
general shadow={fill=gray!60,shadow xshift=1pt,shadow yshift=-1pt}},
spl/.style={cli,append after command={
node[circle,draw,dotted,
minimum size=1.5cm] at (\tikzlastnode.center) {}}},
c1/.style={-stealth,very thick,red!80!black},
v2/.style={-stealth,very thick,yellow!65!black},
v4/.style={-stealth,very thick,purple!70!black}}
\node[dep] (0) at (0,0) {0};
\node[cli] (7) at (-1,-2) {7};
\node[spl] (8) at (2,-2) {8};
\draw[c1] (0) to[bend right] (7);
\draw[v2] (7) -- (8);
\draw[v4] (8) -- (2,-1) -- (0);
\end{tikzpicture}
\end{document}
You can use a matrix or whatever you want to add a legend
\documentclass[tikz,margin=3]{standalone}
\usetikzlibrary{shadows,matrix} % Shadows for nodes
\begin{document}
\begin{tikzpicture}
\tikzset{% This is the style settings for nodes
dep/.style={circle,minimum size=#1,fill=orange!20,draw=orange,
general shadow={fill=gray!60,shadow xshift=1pt,shadow yshift=-1pt}},
dep/.default=1cm,
cli/.style={circle,minimum size=#1,fill=white,draw,
general shadow={fill=gray!60,shadow xshift=1pt,shadow yshift=-1pt}},
cli/.default=1cm,
spl/.style={cli=#1,append after command={
node[circle,draw,dotted,
minimum size=1.5cm] at (\tikzlastnode.center) {}}},
spl/.default=1cm,
c1/.style={-stealth,very thick,red!80!black},
v2/.style={-stealth,very thick,yellow!65!black},
v4/.style={-stealth,very thick,purple!70!black}}
\begin{scope}[local bounding box=graph]
\node[dep] (0) at (0,0) {0};
\node[cli] (7) at (-1,-2) {7};
\node[spl] (8) at (2,-2) {8};
\draw[c1] (0) to[bend right] (7);
\draw[v2] (7) -- (8);
\draw[v4] (8) -- (2,-1) -- (0);
\end{scope}
\matrix[draw,anchor=north west,xshift=1ex,matrix of nodes,row sep=.5ex,
column 2/.style={text width=2.5cm,align=left,anchor=center}]
at (graph.north east) {
\draw[c1] (-.5,0) -- (.5,0); & $C_1$\\
\draw[v2] (-.5,0) -- (.5,0); & $V_2$\\
\draw[v4] (-.5,0) -- (.5,0); & $V_4$\\
\draw[dotted] circle (.25); & Split Delivery\\
\node[cli=.5cm] {}; & Cliente\\
\node[dep=.5cm] {}; & Deposito\\
};
\end{tikzpicture}
\end{document}
It seems that you haven't known much about TikZ yet, then this question on TeX.SE may be useful to you.

graphviz dot to pdf: distance from graph to title

in graphviz dot, generating pdf output.
how do I increase the distance between the title and the graph?
digraph "test" {
graph [ label="need more distance to below" ];
A -> B;
}
advice appreciated.
Not preferable solution but Append "\n" at the end of label. If you need more space add more "\n".

Resources