usetikzlibrary 中的選項

usetikzlibrary 中的選項

是否可以有一個\usetikzlibrary命令選項?我想在啟動選項時定義不同的樣式。例如:

\usetikzlibrary[blue]{something}
\usetikzlibrary[green]{something}
\usetikzlibrary[red]{something}

會為相同的顏色定義不同的顏色

\tikzset{C/.style={thick, circle, fill=red}};

謝謝。

答案1

正如 percusse 和 cfr 的評論所說,您不能使用 tikz 庫的選項。您可以在手冊中讀到,您可以用方括號替換花括號(ConTeXt 特定)。

以下是如何使用.is choice處理程序設定選項的範例。

\documentclass[border=1cm]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}
% ----------- code to put in your  tikzlibrarysomething.code.tex
\tikzset{
  C/.style={ultra thick, circle},
  theme/.is choice,
  theme/ocean/.style={C/.append style={draw=none,fill=blue!50}},
  theme/fire/.style={C/.append style={draw=orange,fill=red}},
  theme/nature/.style={C/.append style={draw=brown,fill=green!70}},
  theme = fire % <- set the default theme
}
%------------- then to use
% \usetikzlibrary{something}
\tikzset{theme=ocean}

%------------- and here we are
\begin{document}
    \tikz \node[C] {Test};
\end{document}

在此輸入影像描述

編輯:在 cfr 的評論之後,我更改了程式碼以使其更加“不言自明”。

答案2

這是 Kpym 答案的改編。雖然 Kpym 的解決方案更接近您所說的需求,但我的解決方案更短且更靈活,因為它允許您選擇任何顏色。

\documentclass[border=5pt,tikz]{standalone}
\usetikzlibrary{calc}

\tikzset{
  C/.style={circle},
  theme color/.style={C/.append style={fill=#1!50}},
}

\tikzset{theme color=magenta}

\begin{document}
  \tikz \node[C] {Test};
\end{document}

但是,您可能希望包含調整不透明度的選項,並且最好確保設定預設值,以防使用者(可能是您)不記得設定顏色或其他內容。

以下是基於克勞迪奧·菲安德里諾的回答並設定顏色和不透明度的預設值,並可以選擇覆蓋它們。如果在序言中使用,則需要\makeatletter... \makeatother@.如果在您的庫代碼中使用,則不需要這樣做。

\tikzset{
  C/.style={circle, fill=\my@theme@color!\my@theme@color@opacity},
  theme color/.store in=\my@theme@color,
  theme color opacity/.store in=\my@theme@color@opacity,
  theme settings/.code={%
    \tikzset{#1}},
  theme color=blue,% set a default
  theme color opacity=50,% set a default
}

如果您什麼都不做,您將看到一個藍色圓圈,不透明度為 50%。但是您可以透過在序言中覆蓋它們來更改這些預設值:

\tikzset{theme settings={theme color=magenta, theme color opacity=75}}

這會給你一個洋紅色圓圈,填充為 75% 不透明度。或者您可以為特定節點或scope圖片中的特定節點覆蓋它們。

前導碼、範圍和節點設定覆蓋預設值

\documentclass[border=5pt,tikz]{standalone}
\usetikzlibrary{calc}

\makeatletter
\tikzset{% https://tex.stackexchange.com/a/159856/ - Claudio Fiandrino
  C/.style={circle, align=center, fill=\my@theme@color!\my@theme@color@opacity},
  theme color/.store in=\my@theme@color,
  theme color opacity/.store in=\my@theme@color@opacity,
  theme settings/.code={%
    \tikzset{#1}},
  theme color=blue,% set a default
  theme color opacity=50,% set a default
}
\makeatother
\tikzset{theme settings={theme color=magenta, theme color opacity=75}}

\begin{document}
  \begin{tikzpicture}
    \node[C] {Preamble\\Config};
    \begin{scope}[theme settings={theme color=yellow, theme color opacity=100}]
      \node [C, xshift=15mm] {Scope\\Config};
    \end{scope}
    \node [C, theme color=green, theme color opacity=15, xshift=29mm] {Node\\Config};
  \end{tikzpicture}
\end{document}

相關內容