4

I used Tikz to draw some specific shapes on a matrix, this the MWE that I am using:

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{matrix,decorations.pathreplacing}
\pgfkeys{tikz/mymatrixenv/.style={decoration=brace,every left delimiter/.style={xshift=4pt},every right delimiter/.style={xshift=-4pt}}}
\pgfkeys{tikz/mymatrix/.style={matrix of math nodes,left delimiter=[,right delimiter={]},inner sep=1pt,row sep=0em,column sep=0em,nodes={inner sep=6pt}}}

\begin{document}

\begin{tikzpicture}[baseline=0cm,mymatrixenv]
    \matrix [mymatrix,text width=0.6em,align=center] (m)  
    {
    a & b & c \\ 
    d & e & f \\
    g & h & i \\
    };
    \pgfmathsetmacro{\offset}{0.5mm}
    \draw [thick,blue,rounded corners=1mm] (m-1-1.west) |- (m-3-3.south) -- cycle;
    \draw [thick,red,rounded corners=1mm] (m-1-1.north) -| (m-3-3.east) -- cycle;
    \draw [thick,green,rounded corners=1mm] ([yshift=\offset]m-1-1.west) -- ([xshift=-\offset]m-1-1.north) -- ([yshift=-\offset]m-3-3.east) -- ([xshift=\offset]m-3-3.south) -- cycle;
\end{tikzpicture}


\end{document}

Result:

enter image description here

but when I tried this code in a beamer presentation (theme: Warsaw) I get this: enter image description here

What's wrong and how can I fix it?

  • Please provide a full minimal example of the beamer use (that makes it much faster for others to test and suggest solutions) – daleif Aug 12 '18 at 12:10
  • Tested with \documentclass{beamer} \usetheme{Warsaw} and works fine on share latex... may be you need a second run if didn't tried – koleygr Aug 12 '18 at 12:40
  • I think it would be appropriate if you mentioned that you got that nice code from here. – user121799 Aug 12 '18 at 15:51
10

You need to use a fragile frame:

\documentclass{beamer}

\usepackage{tikz}
\usetikzlibrary{matrix,decorations.pathreplacing}
\pgfkeys{tikz/mymatrixenv/.style={decoration=brace,every left delimiter/.style={xshift=4pt},every right delimiter/.style={xshift=-4pt}}}
\pgfkeys{tikz/mymatrix/.style={matrix of math nodes,left delimiter=[,right delimiter={]},inner sep=1pt,row sep=0em,column sep=0em,nodes={inner sep=6pt}}}

\begin{document}
\begin{frame}[fragile]

\begin{tikzpicture}[baseline=0cm,mymatrixenv]
    \matrix [mymatrix,text width=0.6em,align=center] (m)  
    {
    a & b & c \\ 
    d & e & f \\
    g & h & i \\
    };
    \pgfmathsetmacro{\offset}{0.5mm}
    \draw [thick,blue,rounded corners=1mm] (m-1-1.west) |- (m-3-3.south) -- cycle;
    \draw [thick,red,rounded corners=1mm] (m-1-1.north) -| (m-3-3.east) -- cycle;
    \draw [thick,green,rounded corners=1mm] ([yshift=\offset]m-1-1.west) -- ([xshift=-\offset]m-1-1.north) -- ([yshift=-\offset]m-3-3.east) -- ([xshift=\offset]m-3-3.south) -- cycle;
\end{tikzpicture}
\end{frame}

\end{document}

enter image description here

  • Do you have a slight idea what difference the option fragile makes (internally)? – Dr. Manuel Kuehner Aug 12 '18 at 13:30
  • 1
    @Dr.ManuelKuehner Only a very rough idea. It more or less treats the content of the frame as verbatim material and limits the amount of processing which is normally done. For tikz the most noticeable effect is that tikz can still use one #. Without the fragile option one would need ###. – user36296 Aug 12 '18 at 14:15
  • Thanks! That helps. Wouldn't it be a a good idea then, to set fragile as the standard frame option? Apart from a maybe slower compilation process. Or is this rather a own question? – Dr. Manuel Kuehner Aug 12 '18 at 17:22
  • 1
    @Dr.ManuelKuehner No. Having fragile as standard option causes quite some problems. It should only be used if necessary. – user36296 Aug 12 '18 at 18:41
  • 1
    @Dr.ManuelKuehner To give a few examples: it interact badly with other options like shrink or allowframebreaks. – user36296 Aug 13 '18 at 13:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.