1

Is it possible that an (already existing) style adds a prefix to every label using this style?

So the label in the following example should be L:label.

\documentclass[tikz]{standalone}
\tikzstyle{withPrefix}=[] %add "L:" before every label with this option

\begin{document}

\begin{tikzpicture}
\node [draw=black] [label={[withPrefix]label}] {};
\end{tikzpicture}

\end{document}

Clarification:

I use \usetikzlibrary{quotes} and want to move the L{:} from the label text into the option.

\documentclass[tikz]{standalone}
\usetikzlibrary{quotes}
\tikzstyle{future}=[red] %add "L:" before every label with this option
\tikzstyle{now}=[red]

\begin{document}

\begin{tikzpicture}
\node at (0,0)    [draw] ["label" future] {a};

\node at (0,-1.5) [draw] ["L{:}label" now] {b};
\end{tikzpicture}

\end{document}
user1
  • 2,196
  • 1
    try if the following gives what you like to have: `\documentclass[tikz]{standalone}

    \begin{document} \begin{tikzpicture}[ box/.style args = {#1:#2}{draw, label=#1: L #2} ] \node [box=above:label] {}; \end{tikzpicture} \end{document}`

    – Zarko Jan 24 '19 at 13:30
  • 1
    or the following: `\documentclass[tikz]{standalone}

    \begin{document} \begin{tikzpicture}[ lbl/.style args = {#1:#2}{label={[font=\scriptsize]#1:\textcolor{red}{L}~#2}} ] \node [draw, lbl=below:label] {}; \end{tikzpicture} \end{document}`

    – Zarko Jan 24 '19 at 13:37
  • @Zarko I don't want the labels to look any different from normal labels, so something like {label=#1:L:#2} should be correct, I guess. But as I use the quotes tikzlibrary (removed this by minimizing the example) and actually wanted to write "label" withPrefix to produce L:label this does not seem to work – user1 Jan 24 '19 at 14:28
  • both examples in my comments work. please, be so kind and show an sketch, what you like that labels look like. your approach (as far as i can understand your mwe and description) will not give expected result. – Zarko Jan 24 '19 at 14:50
  • @Zarko please see my update – user1 Jan 24 '19 at 15:26
  • what is more important to you: that you for labels you use quotes (which is not intended for labeling of nodes) or that you have simple standard solution which works? – Zarko Jan 24 '19 at 15:39
  • I like the short notation from quotes, but modifying your comments just a little led my to my solution below (which is short in use). – user1 Jan 25 '19 at 09:48

2 Answers2

1

You could abuse font for that. (There is an alternative key, node font.)

\documentclass[tikz]{standalone}
\usetikzlibrary{quotes}
\tikzset{future/.style={red,font=L:}, %add "L:" before every label with this option
now/.style={red}}

\begin{document}

\begin{tikzpicture}
\node at (0,0)    [draw] ["label" future] {a};

\node at (0,-1.5) [draw] ["L{:}label" now] {b};
\end{tikzpicture}

\end{document}

enter image description here

  • and you put label below of node? – Zarko Jan 24 '19 at 15:44
  • @marmot should tikzset always be preferred? – user1 Jan 24 '19 at 16:02
  • @Ben This is what one may conclude from this discussion. I think in your above example it does not really matter because you seem to want the styles to be global. My rationale for weeding \tikzstyle out is that there seems to be no advantage using it, and there are cases where using it hurts. –  Jan 24 '19 at 16:05
  • huh, i lost one word in my comment :-(, correct is : and how you put a label below of node? – Zarko Jan 24 '19 at 16:13
  • @Zarko Was that asked? You could either do \node at (0,0) [draw] ["label" {future,below}] {a}; or add it to the definition of the style: \tikzset{future/.style={red,font=L:,below}}. –  Jan 24 '19 at 16:16
  • nice. thank you very much! however i prefer solution which i propose in my second solution ... – Zarko Jan 24 '19 at 16:24
1

I'm providing an own answer mainly basing on Zarko's comments.

\documentclass[tikz]{standalone}
\tikzset{%
future/.style args = {#1}{label={[red]below right:L:#1}}}

\begin{document}

\begin{tikzpicture}
\node [draw,future=label] {};
\end{tikzpicture}

\end{document}

the output

user1
  • 2,196