在rmarkdown的tikz节点标签中使用项目符号列表 [英] Using bullets list in tikz's node label in rmarkdown

查看:139
本文介绍了在rmarkdown的tikz节点标签中使用项目符号列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个tikz代码来生成一些在latex中正常工作的图形(在背面进行了测试: https://www.overleaf.com ).但是,这应该是大型rmarkdown文件的一部分,但是在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:

  • 如何在rmarkdown中使用项目符号列表作为tikz的节点标签?
  • 如何自定义该列表以包括颜色,边距等格式?
  • 使用rmarkdownlatex中定义的内容在哪里?
  • 在使用rmarkdown时在\setlist中定义的内容在哪里?
  • 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?

我可以在latex中生成这些图形,并使用knitr :: include_graphics(...)将其包括在内,但我更喜欢使用一种更自动的方式,即我可以让代码生成图形并在出现图形时将它们嵌入进入文件.

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}

当该标签不涉及项目列表时,它也可以在编织块内作为节点标签使用.否则,结果为:! LaTeX Error: Something's wrong--perhaps a missing \item.

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}

tikz2pdf.tex包含以下内容:

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}

最终,我想自定义此列表以更改项目的格式,例如颜色,边距等.为此,我有以下代码也可以在latex中使用,但是我不确定将其放置在何处使用rmarkdown时.

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}
    }

理想情况下,我希望能够像在latex中那样使用它:

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

  • 第一项
  • 第二项

在节点标签中.

推荐答案

Alain Merigot解决了您的Tikz问题.您的其他问题与R Markdown特别相关.我会尽力解决这些问题.

Alain Merigot solved your Tikz problem. Your other problems are related to R Markdown specifically. I'll try to address those.

您在评论中询问了他给您的代码放在哪里.为了解决这个问题,您需要考虑一下R Markdown过程. tikz代码块中的所有代码都将插入到tikz2pdf.tex中您拥有%% TIKZ_CODE %%的行.但是您没有该行,因此需要添加它.然后,LaTeX将处理该文件以生成图形.因此,图中将使用的所有内容都必须放入您的tikz2pdf.tex模板中.那是Alain的大部分补充.

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.

LaTeX生成图形后,它将在您的主文件中再次调用它,该文件将具有某种\includegraphics宏以将图形包括在主文件中.如果您还希望主文件中有任何这些定义(例如,两个地方都希望使用相同的颜色),则也必须在其中重复这些定义.它们应该放在代码块之外.如果它们需要放在标题中(例如\usepackage调用),则需要 在YAML中.

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.

这是使用Alain的Tikz代码对示例进行的修改.

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}
```

这将产生一个包含

注意:在编辑模板时,我在代码块中设置了cache = FALSE.如果不这样做,除非tikz块也有所更改,否则knitr不会查看模板的更改.停止缓存可以避免开发过程中的混乱.对模板满意后,您可以再次打开缓存以加快处理速度.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆