在rmarkdown中使用tikz节点标签的项目符号列表

3

我有一个tikz代码用于生成一些图形,在latex中可以正常工作(已在overleaf测试:https://www.overleaf.com)。然而,这应该是一个大型的rmarkdown文件的一部分,但我似乎无法在rmarkdown中使用节点标签的项目符号列表。我的问题如下:

  • 如何在rmarkdown中使用带有项目符号列表的tikz的节点标签?
  • 如何自定义该列表以包括一些格式设置,如颜色、边距等?
  • 使用rmarkdown时,使用\newlist\setlistlatex中定义的内容会放到哪里?

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

    ---
    title: "Title"
    author: "Me"
    output:
      bookdown::pdf_document2:
        keep_tex: yes
        latex_engine: xelatex
    ---

以下内容在 knitr 代码块外部运行正常。
    p
    \begin{itemize}
    \item first item
    \item second item
    \end{itemize}

当节点标签不涉及项目列表时,它也可以作为knitr代码块内的节点标签。否则,它会导致以下错误:! 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文件的内容如下:

    \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时应将其放在哪里。

    \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中一样使用它: latex
    \node [stile](P){
    p
    \begin{myBullets}
    \item first item
    \item second item
    \end{myBullets}
    };

我希望(抱歉我无法提供完整的图片),输出结果应该类似于:

P

  • 第一项
  • 第二项

在节点标签中。

2个回答

4

如果您想在带有段落、换行等的节点中拥有复杂的文本,则需要将其放入类似于parbox或minipage的框中。否则,tikz无法确定文本宽度并执行任何格式化。

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

enter image description here

我将您的BulletColor更改为文本几乎是白色和不可见的。

嗨@Alain!添加minipage环境解决了错误:!LaTeX错误:可能缺少\item,如果我只使用**\begin{itemize}...\end{itemize}。我仍然不知道样式应该放在R markdown中的哪里。添加\newlist{myBullets}{itemize}{1}\setlist[myBullets]{...}会导致:!未定义的控制序列。l.16 \setlist[myBullets]{ **。你能帮忙吗? - Liman
我的问题的第二部分与R Markdown有关,如下所示已解决。由于声望不够,我无法为您点赞,但是一旦我达到要求,您就可以期待我的赞。谢谢! - Liman

1

Alain Merigot解决了你的Tikz问题。你的其他问题涉及特定于R Markdown的问题。我会尝试解决这些问题。

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

LaTeX生成图形后,将再次调用您的主文件,在主文件中包含某种\includegraphics宏来包含该图形。如果您还希望在主文件中使用这些定义(例如,您希望两个位置使用相同的颜色),则必须在那里重复定义。它们应该放在代码块之外。如果它们需要在标题中(例如\usepackage调用)出现,则需要在YAML中进行设置。
以下是使用Alain的Tikz代码修改您的示例。

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

这句话的意思是:这会生成一个包含“

”的页面。

screenshot

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

嗨@user2554330,这解决了我的问题,感谢您的澄清!我接受了这个答案! - Liman

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接