6

I was attempting to place arrows to parts of an equation, however my output is very untidy...the equation is offset and the arrows do not go into the tcolorbox:

enter image description here

Can you help me get the arrows go inside the tcolorbox and have the equation not offset like that shown in the image above?

Here is my code:

\documentclass{article}

\usepackage{xcolor}
\usepackage{tikz}
\usetikzlibrary{tikzmark,calc,,arrows,shapes,decorations.pathreplacing}
\tikzset{every picture/.style={remember picture}}

\usepackage{amsmath}
\usepackage[framemethod=default]{mdframed}
\usepackage{mathtools}
\usepackage[most]{tcolorbox}
\tcbset{myformula/.style={
  arc=0pt,
  outer arc=0pt,
  colback=gray!30,
  colframe=black,
  boxrule=0.8pt,
  left=2pt,
  right=2pt,
  highlight math style={
    arc=0pt,
    outer arc=0pt,
    colback=gray,
    colframe=blue.
    }
  }
}

\begin{document}
\begin{enumerate}
\item The minimum settling time is given as
\begin{tcolorbox}[ams equation, myformula]
\text{Minimum Settling Time } = (%
\tikz[baseline]{\node(d1) {$q$}} +%
\tikz[baseline]{\node(d2) {$\text{max}\{\ell -1, 0 \}$}} + 1)T
\end{tcolorbox}
\begin{tikzpicture}[remember picture,overlay]{
   \draw[blue,thick,latex-,rounded corners] (d1) |- ++ (0.6cm,-1.5cm) node[anchor=west,text = black,] (label1) {input\\order};
   \draw[blue,thick,latex-,rounded corners] (d2) |- ($(label1.west)-(-0.8cm,-0.6cm)$) node[anchor=west,text = black] {G(z)};}
\end{tikzpicture}
\vspace{1cm}

\item This is another item

\end{enumerate}
\end{document}
karlkoeller
  • 124,410
Joe
  • 9,080

2 Answers2

6

In my opinion, the arrows should be added as an overlay= option to the tcolorbox, not as a separate environment.

\documentclass{article}

\usepackage{xcolor}
\usepackage{tikz}
\usetikzlibrary{tikzmark,calc,,arrows,shapes,decorations.pathreplacing}
\tikzset{every picture/.style={remember picture}}

\usepackage{amsmath}
\usepackage[framemethod=default]{mdframed}
\usepackage{mathtools}
\usepackage[most]{tcolorbox}
\tcbset{myformula/.style={
  arc=0pt,
  outer arc=0pt,
  colback=gray!30,
  colframe=black,
  boxrule=0.8pt,
  left=2pt,
  right=2pt,
  highlight math style={
    arc=0pt,
    outer arc=0pt,
    colback=gray,
    colframe=blue.
    }
  }
}

\begin{document}
\begin{enumerate}
\item The minimum settling time is given as
  \begin{tcolorbox}[enhanced,ams equation, myformula,overlay={
      \draw[blue,thick,latex-,rounded corners] (d1) |- ++ (0.6cm,-1.5cm) node[anchor=west,text = black,] (label1) {input\ order};
      \draw[blue,thick,latex-,rounded corners] (d2) |- ($(label1.east)-(-0.8cm,-0.6cm)$) node[anchor=west,text = black] {G(z)};}
    ]
\text{Minimum Settling Time } = (%
\tikz[baseline=d1.base]{\node(d1) {$q$}} +%
\tikz[baseline=d2.base]{\node(d2) {$\text{max}\{\ell -1, 0 \}$}} + 1)
\end{tcolorbox}

\item This is another item

\end{enumerate}
\end{document}

enter image description here

  • Thanks for your solution! The baseline alignment of the equation is still offset, i.e., the q is not aligned to the open parenthesis ( as the 1 is to the close parenthesis )...is there a way to fix this? – Joe Jun 17 '17 at 18:51
  • 2
    @Joe Instead of [baseline] use [baseline=(d1.base)] for the node d1 and do the same for d2. – karlkoeller Jun 17 '17 at 19:05
  • @karlkoeller, thanks, there is still one more issue...if I compile this is XeLaTeXify, the arrows are still outside the box, can this code be made to run in XeLaTeXify instead of PDFLaTeX? Thanks – Joe Jun 17 '17 at 19:15
  • @Joe I really don't know why xelatex gives that output... – karlkoeller Jun 17 '17 at 19:23
  • @Joe The output is fine with lualatex, if this is an option for you. – karlkoeller Jun 17 '17 at 19:31
  • 1
    @Joe: Added karlkoeller's proposition, he was quicker than me ;-) –  Jun 17 '17 at 19:37
6

I would suggest not to draw parts of the formula by hands and let LaTeX render it. The problem with your formula is that it's hard to get correct spacing. As for the arrows, you can use tikzmarks to label the place on the page where to point them. The following example does that (I borrowed using the tcolorbox overlay idea from the Christian's answer):

\documentclass{article}

\usepackage{xcolor}
\usepackage{tikz}
\usetikzlibrary{tikzmark,calc,,arrows,shapes,decorations.pathreplacing}
\tikzset{every picture/.style={remember picture}}

\usepackage{amsmath}
\usepackage[framemethod=default]{mdframed}
\usepackage{mathtools}
\usepackage[most]{tcolorbox}
\tcbset{myformula/.style={
  arc=0pt,
  outer arc=0pt,
  colback=gray!30,
  colframe=black,
  boxrule=0.8pt,
  left=2pt,
  right=2pt,
  highlight math style={
    arc=0pt,
    outer arc=0pt,
    colback=gray,
    colframe=blue.
    }
  }
}

\begin{document}
\begin{enumerate}
\item The minimum settling time is given as
\begin{tcolorbox}[enhanced,ams equation, myformula,overlay={
  \draw[blue,thick,latex-,rounded corners] (pic cs:d1)+(3pt,-1.1ex) |- ++ (0.6cm,-1.5cm) node[anchor=west,text=black] (label1) {input\ order};
  \draw[blue,thick,latex-,rounded corners] (pic cs:d2)+(3pt,-1.1ex) |- ($(label1.east)-(-0.8cm,-0.6cm)$) node[anchor=west,text=black] {G(z)};}
  ]
  \text{Minimum Settling Time} = (\tikzmark{d1}q+\max\{\ell\tikzmark{d2}-1,0\}+1)T
\end{tcolorbox}
\vspace{1cm}

\item This is another item

\end{enumerate}
\end{document}

This example still doesn't work with XeLaTeX. pdfLaTeX and LuaLaTeX work fine.

formula

  • I came across this link: https://tex.stackexchange.com/questions/229500/tikzmark-and-xelatex, I do not know how to implement the solution in this case. It appears that there is an offset between pdfLaTeX and XeLaTex. – Joe Jun 17 '17 at 21:53
  • 1
    Right, so if you don't care about pdflatex and lualatex and will use xelatex only, you can manually add some shift. Using ($(pic cs:d1)+(8pt,2.2ex)$) for the first arrow and similar expression for the second one fixes arrow placement for me. – Sergei Golovan Jun 18 '17 at 05:44