Infering the size of a minipage from the containin

2019-08-02 04:19发布

问题:

This is a follow-up question to Using bullets list in tikz's node label in rmarkdown. I had some TikZ code that works fine in pure LaTex but NOT when I transport it to rmarkdown where the error ! LaTeX Error: Something's wrong--perhaps a missing \item. is raised. This was solved in the answer to Using bullets list in tikz's node label in rmarkdown but another issue arises applying the solution I got in there.

You can refer to the original question (Using bullets list in tikz's node label in rmarkdown) but basically I have some TikZ code for pictures to be used as part of a larger rmarkdown file. It works in LaTex as I tested in on https://www.overleaf.com/ but once in rmarkdown, it raises the missing item error. The proposed solution in Using bullets list in tikz's node label in rmarkdown was to add a \minipage environment in rmarkdown (see the code below).

My problem with the use of the \minipage environment is that I will have to manually set its width (or at least I don't know how to automate this) before creating the node which is supposed to be part of a large TikZ picture. In other words, I need to know the allocated space for each node to reproduce the picture in rmarkdown. I was wondering whether there is a way to infer the size of the node in advance, so that I can create a minipage matching the size of the node it will contain.

\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 am also open to other solutions as long as I will NOT have to specify the size of my nodes manually. For example doing (note the commented lines)

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

in TikZ will infer the size of the node from its text size and I am looking for something that allows me to use the same code in rmarkdown without having to manually specify the size of each minipage across my nodes.

回答1:

You could replace minipage with the varwidth environment from the package of the same name:

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