3

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.

Liman
  • 1,270
  • 6
  • 12
  • 1
    Probably unrelated to the problem, but the use of a minipage in the tikz node seems overkill. I suggest to simply specify the text width, e.g. `\node[draw, rounded corners,text width = 3cm] (a){ p \begin{myBullets} \item first item \item second item \end{myBullets} };` – samcarter_is_at_topanswers.xyz Jun 17 '19 at 15:08
  • 1
    "Infering the size of a minipage from the containing tikz node for rmarkdown" You can't, if you are in paragraph mode. A paragraph takes as much space as it can before line breaking. So a paragraph, without any mean to compute its size is a TeX error. – Alain Merigot Jun 17 '19 at 15:10
  • 1
    If you dont want the minipage, you can use `\node[draw,text width=3cm](a){P, etc}` If you dont want the fixed width, replace it by `1.2\widthof{my item width}` that will scale with your fonts (with the calc package). But you need to introduce some width. And a last word. Your documents will probably look nicer if all nodes have the same size (or you have a limited set of differents sizes). So even if it was possible, I do not think it woud be a good idea to have node width completely adapt to the actual text. – Alain Merigot Jun 17 '19 at 15:10
  • @samcarter, this works fine in rmarkdown but I also need my style (set up as stile namely) – Liman Jun 17 '19 at 17:44
  • @Alain Merigot, I will give a try to the same size node idea. I haven't even thought about having the same node size may give a better look to the whole picture. – Liman Jun 17 '19 at 17:51
  • 1
    @Liman Did you try to add `namely` to the node options? – samcarter_is_at_topanswers.xyz Jun 17 '19 at 18:39
  • @samcarter Thank you for answer and comments. I have been trying to test it and make sure it solves my problem before accepting it. I just added my style and it works though with some warnings. Basically, instead of `\node[draw, rounded corners, font=\itshape, text=BulletsColor] (a) {...}`, I just used `\node[stile] (a) {...}`. – Liman Jun 17 '19 at 22:17
  • @samcarter I am not aware of `namely` as node option. Can you elaborate? – Liman Jun 17 '19 at 22:21
  • @Liman I understood your comment as if your style was called `namely`, but apparently it is called something else. – samcarter_is_at_topanswers.xyz Jun 17 '19 at 22:23
  • @samcarter Oh! I called it `stile`. – Liman Jun 17 '19 at 22:25
  • 1
    @Liman I have yet to learn how to read minds :) – samcarter_is_at_topanswers.xyz Jun 17 '19 at 22:26
  • @samcarter fair enough! :) – Liman Jun 17 '19 at 22:51

1 Answers1

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}

enter image description here