在 rmarkdown 中使用 tikz 节点标签中的项目符号列表 [英] Using bullets list in tikz's node label in rmarkdown
问题描述
I have a tikz code to generate some graphs that works properly in latex
(tested on overleaf: https://www.overleaf.com). However, this is supposed to be part of a large rmarkdown
file but I cannot seem to use bullets list in some of my node labels while in rmarkdown
. My questions are:
- how can I use a list of bullets as tikz's node label in
rmarkdown
? - how can I customize that list to include some formatting like color, margin etc.?
- Where do things defined in
latex
usingewlist
,setlist
go when usingrmarkdown
?
I could generate these figures in latex
and include them using knitr::include_graphics(...) but I prefer to use a more automatic way where I could just let the code generates the figures and embeds them as they arise into the file.
---
title: "Title"
author: "Me"
output:
bookdown::pdf_document2:
keep_tex: yes
latex_engine: xelatex
---
The following works fine outside a knitr chunk.
p
egin{itemize}
item first item
item second item
end{itemize}
It will also work inside the knitr chunk as node label when that label does not involve the item list. Otherwise, it results in: ! LaTeX Error: Something's wrong--perhaps a missing item.
```{tikz, tikz-ex, echo=F, fig.cap = "Funky tikz", fig.ext = 'png', cache=TRUE, eval=T, engine.opts = list(template = "tikz2pdf.tex")}
usetikzlibrary{arrows, shapes}
definecolor{myColor}{rgb}{0.98, 0.94, 0.9}
egin{tikzpicture}
ikzstyle {stile} = [
ellipse,
draw=myColor,
fill=myColor,
thick,
inner sep=0pt,
text centered,
align=center
]
ode [stile](P){
p
egin{itemize}
item first item
item second item
end{itemize}
};
end{tikzpicture}
The tikz2pdf.tex contain is the following:
documentclass{article}
include{preview}
usepackage[utf8]{inputenc}
usepackage[skins]{tcolorbox}
usepackage{
tikz,
enumitem,
xcolor
}
usetikzlibrary{
shapes,
arrows
}
egin{document}
egin{preview}
end{preview}
end{document}
Ultimately, I would like to customize this list to change the formatting of the items such as colors, margins etc. For this, I have the following code that also works in latex
but I am not sure where to put it when using rmarkdown
.
definecolor{BulletsColor}{rgb}{0.98, 0.94, 0.9}
ewlist{myBullets}{itemize}{1}
setlist[myBullets]{
label= extcolor{BulletsColor}{ extbullet},
leftmargin=*,
topsep=0ex,
partopsep=0ex,
parsep=0ex,
itemsep=0ex,
before={color{BulletsColor}itshape}
}
Ideally, I would like to be able to use this just as I would do in latex
as:
ode [stile](P){
p
egin{myBullets}
item first item
item second item
end{myBullets}
};
I expect(and sorry I could not provide a complete picture) the output to be something like :
P
- first item
- second item
in the node label.
Alain Merigot solved your Tikz problem. Your other problems are related to R Markdown specifically. I'll try to address those.
You asked in a comment where to put the code he gave you. To work that out, you need to think about the R Markdown process. All of the code in your tikz
code chunk will be inserted into your tikz2pdf.tex
at the line where you have %% TIKZ_CODE %%
. But you don't have that line, so you need to add it. Then LaTeX will process that file to produce the figure. So anything that will be used in the figure has to go into your tikz2pdf.tex
template. That's most of Alain's additions.
After LaTeX produces the figure, it will be called again on your main file, which will have some sort of includegraphics
macro to include the figure in your main file. If you also want any of those definitions in the main file (e.g. you want the same colour in both places), you would have to repeat the definitions there as well. They should go outside of a code chunk. If they need to be in a header (e.g. usepackage
calls), those would need to
be in the YAML.
Here are modifications of your example using Alain's Tikz code.
tikz2pdf.tex
:
documentclass{article}
usepackage[pdftex,active,tightpage]{preview}
usepackage[utf8]{inputenc}
usepackage[skins]{tcolorbox}
usepackage{
tikz,
enumitem,
xcolor
}
egin{document}
definecolor{BulletsColor}{rgb}{0, 0, 0.9}
definecolor{myColor}{rgb}{0.98, 0.94, 0.9}
ewlist{myBullets}{itemize}{1}
setlist[myBullets]{
label= extcolor{BulletsColor}{ extbullet},
leftmargin=*,
topsep=0ex,
partopsep=0ex,
parsep=0ex,
itemsep=0ex,
before={color{BulletsColor}itshape}
}
ikzstyle {stile} = [
ellipse,
draw=BulletsColor,
fill=myColor,
thick,
inner sep=0pt,
text centered,
align=center
]
egin{preview}
%% TIKZ_CODE %%
end{preview}
end{document}
Title.Rmd
:
---
title: "Title"
author: "Me"
output:
bookdown::pdf_document2:
keep_tex: yes
latex_engine: xelatex
---
```{tikz tikz-ex, echo=FALSE, fig.cap = "Funky tikz", fig.ext = 'pdf', cache=FALSE, eval=TRUE, engine.opts = list(template = "tikz2pdf.tex")}
usetikzlibrary{
shapes,
arrows
}
egin{tikzpicture}
ode[stile] (a){
egin{minipage}{2.5cm}
p
egin{myBullets}
item first item
item second item
end{myBullets}
end{minipage}
};
end{tikzpicture}
```
This produces a page containing
Note: I set cache = FALSE
in the code chunk while I was editing the template. If you don't, knitr
won't look at changes to the template unless there are also changes to the tikz
chunk. Stopping caching avoids confusion during development. Once you're happy with the template, you can turn caching on again to speed things up.
这篇关于在 rmarkdown 中使用 tikz 节点标签中的项目符号列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!