Using bullets list in tikz's node label in rmarkdown

不想你离开。 提交于 2019-12-02 12:24:55

问题


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 using \newlist, \setlist go when using rmarkdown?

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
    \begin{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}

    \begin{tikzpicture}

    \tikzstyle {stile} = [
    ellipse,
    draw=myColor,
    fill=myColor,
    thick,
    inner sep=0pt,
    text centered, 
    align=center
    ]

    \node [stile](P){
    p
    \begin{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
    }

    \begin{document}

    \begin{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}
    \newlist{myBullets}{itemize}{1}

    \setlist[myBullets]{
    label=\textcolor{BulletsColor}{\textbullet},
    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:

    \node [stile](P){
    p
    \begin{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.


回答1:


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
}
\begin{document}

\definecolor{BulletsColor}{rgb}{0, 0, 0.9}
\definecolor{myColor}{rgb}{0.98, 0.94, 0.9}
\newlist{myBullets}{itemize}{1}

\setlist[myBullets]{
  label=\textcolor{BulletsColor}{\textbullet},
  leftmargin=*,
  topsep=0ex,
  partopsep=0ex,
  parsep=0ex,
  itemsep=0ex,
  before={\color{BulletsColor}\itshape}
}

\tikzstyle {stile} = [
ellipse,
draw=BulletsColor,
fill=myColor,
thick,
inner sep=0pt,
text centered, 
align=center
]

\begin{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
}
\begin{tikzpicture}

\node[stile] (a){
\begin{minipage}{2.5cm}
p
\begin{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.




回答2:


If you want to have complex text in a node with paragraph, line breaking, etc, you need to put it in a box like a parbox or minipage. Otherwise, tikz has no way to determine text width and to perform any kind of formatting.

\documentclass{article}

\usepackage{tikz}
\usepackage{enumitem}

\begin{document}
\definecolor{BulletsColor}{rgb}{0, 0, 0.9}
\newlist{myBullets}{itemize}{1}

\setlist[myBullets]{
  label=\textcolor{BulletsColor}{\textbullet},
  leftmargin=*,
  topsep=0ex,
  partopsep=0ex,
  parsep=0ex,
  itemsep=0ex,
  before={\color{BulletsColor}\itshape}
}


\begin{tikzpicture}
  \node[draw, rounded corners] (a)  {
    \begin{minipage}{2.5cm}
      p
      \begin{myBullets}
      \item first item
      \item second item
      \end{myBullets}
    \end{minipage}
  }
  ;
  \end{tikzpicture}
\end{document}

I changed your BulletColor as the text was almost white and invisible.



来源:https://stackoverflow.com/questions/56616781/using-bullets-list-in-tikzs-node-label-in-rmarkdown

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!