首页 > 解决方案 > 从 rmarkdown 的包含 tikz 节点推断 minipage 的大小

问题描述

这是在 rmarkdown 的 tikz 节点标签中使用项目符号列表的后续问题。我有一些TikZ代码在纯代码中可以正常工作,LaTex但当我将其传输到引发rmarkdown错误的地方时却不行。! LaTeX Error: Something's wrong--perhaps a missing \item.这在 rmarkdown 中使用 tikz 的节点标签中的项目符号列表的答案中得到了解决,但是应用我在那里得到的解决方案会出现另一个问题。

您可以参考原始问题(Using bullets list in tikz's node label in rmarkdown),但基本上我有一些TikZ图片代码可用作较大rmarkdown文件的一部分。LaTex正如我在https://www.overleaf.com/上测试的那样,它可以正常工作,但是一旦进入rmarkdown,它就会引发丢失项目错误。在 rmarkdown 中使用 tikz 的节点标签中的项目符号列表中提出的解决方案是在其中添加一个\minipage环境rmarkdown(参见下面的代码)。

我使用环境的问题是,在创建应该是大图片\minipage一部分的节点之前,我必须手动设置它的宽度(或者至少我不知道如何自动化) 。TikZ换句话说,我需要知道为每个节点分配的空间来重现rmarkdown. 我想知道是否有办法提前推断节点的大小,以便我可以创建一个与它将包含的节点大小相匹配的小型页面。

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

只要我不必手动指定节点的大小,我也愿意接受其他解决方案。例如做(注意注释行)

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

inTikZ将根据其文本大小推断节点的大小,我正在寻找允许我使用相同代码的东西,rmarkdown而无需手动指定节点上每个 minipage 的大小。

标签: rlatexr-markdownknitrtikz

解决方案


您可以用同名包中的环境minipage替换:varwidth

\documentclass{article}

\usepackage{tikz}
\usepackage{enumitem}

\usepackage{varwidth}


\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, font=\itshape, text=BulletsColor] (a)  {
    \begin{varwidth}{\textwidth}
      p
      \begin{myBullets}
      \item \textcolor{BulletsColor}{first item}
      \item \textcolor{BulletsColor}{second item}
      \end{myBullets}
    \end{varwidth}
  }
  ;
  \end{tikzpicture}
\end{document}

在此处输入图像描述


推荐阅读