3

I have a beamer with many figures. The problem is that now I want something that requires settings that are in contradiction with what was defined beforehand.

I have : - A main file where all the frames are - A command file ( command definition and package loading) - A file per figure ( that are \input in the main)

Particularly, in the command file I have

\usepackage{tikz}
\usetikzlibrary{decorations.pathreplacing}
\tikzset{node distance=5cm, every edge quotes/.append style={midway, below}}

That worked well until now as all figures had the same architecture.

Now I have created another figure ( decision tree) that has those settings:

\usepackage{forest}
\tikzset{
decision/.style={rectangle, minimum height=3pt, minimum width=3pt, draw=black, fill=black, thick, inner sep=0pt},
chance/.style={circle, minimum width=3pt, draw=black, fill=black, thick, inner sep=0pt}
 }

\forestset{
 declare toks={optimality}{},
 declare toks={edge label below}{},
 }

This might well be a N00B question but I was wondering how to "load" package and define stuff such as \tikzset for only one figure. Couldn't find a solution.

Thanks in advance for your help !

  • 2
    This is why you should make these things local, e.g. by saying \begin{tikzpicture}[decision/.style={rectangle, minimum height=3pt, minimum width=3pt, draw=black, fill=black, thick, inner sep=0pt}, chance/.style={circle, minimum width=3pt, draw=black, fill=black, thick, inner sep=0pt}] ... \end{tikzpicture}. –  May 02 '19 at 15:39
  • Thanks very much ! That did it. – Thomartingale May 02 '19 at 16:01
  • @Zarko I think you wanted to say \tikzset{<name>/.style={...}}. (sorry, a late comment) – muzimuzhi Z Dec 02 '21 at 23:31
  • 1
    @muzimuzhiZ, you are right! Quite embarrassing :-(. Thank you very much to note me. I will delete my comment and here replace it with correct one: *you can gives names to your \tikzset as follows: : \tikzset{<style name/.style = {... list of styles ...} } and than use it \begin{tikzpicture}[ – Zarko Dec 02 '21 at 23:41

1 Answers1

3

Welcome to TeX-SE! There is a huge, related discussion under this thread. In any case, TikZ behaves pretty much like LaTeX (which is no surprise, of course, as it based on LaTeX) in that definitions are local unless you make them explicitly global. So if you define

\def\XYZ{123}

inside a group, it will be only known inside the group. (You should avoid using \def in favor of \newcommand but this is a different story.) The analogous syntax is to execute \tikzset in a group, or even easier pass the definitions to the options of the tikzpicture. So with reference to your question, you may want to do something like

\begin{tikzpicture}[decision/.style={rectangle, minimum height=3pt, minimum width=3pt, draw=black, fill=black, thick, inner sep=0pt}, chance/.style={circle, minimum width=3pt, draw=black, fill=black, thick, inner sep=0pt}] 
... 
\end{tikzpicture}

so that the styles are defined locally. If you had defined styles with the same name "outside" they will be overwritten locally but only there.

Very often one has a style that one wants to more or less adopt but with small local variations. One could imagine that you want to have

 \tikzset{decision/.style={rectangle, minimum height=3pt, minimum width=3pt, draw=black, fill=black, thick, inner sep=0pt}}

in the full document but locally you wish to, say, alter inner sep. This can be achieved via

\begin{tikzpicture}[decision/.append style={inner sep=2pt}]
  ... 
\end{tikzpicture}

which just changes inner sep by overwriting it but keeps everything else as it was.

Another thing that may be useful is to locally overwrite styles in standalone documents if they are compiled on their own. Then you may say something like

 \ifstandalone
  \tikzset{my style/.style={line width=3pt,-latex}}
 \fi

which defines the style if the standalone is compiled on its own but not when included in a master document via \includestandalone{subfile.tex}. This allows you to, say, universally redefine the appearance of, say, some arrows but you will still be able to compile and test small pieces of a longer document.

Notice that in LaTeX you have the possibility to make definitions global, e.g. with

 \global\def\XYZ{123}

or

 \gdef\XYZ{123}

or

 \gxdef\XYZ{123}

which do in this case all the same (but in general \xdef is different from \gdef in that it expands the definition). This is not possible with TikZ (unless you are very courageous and set \globaldefs1 but I personally do not recommend that). There is also a related discussion on smuggling which you might be interested in at a given point.

All these statements apply to TikZ descendants like forest (\forestset), pgfplots (\pgfplotsset), smartdiagram (\smartdiagramset) and so on. They all put their stuff in their own directories, which brings us back to this discussion.