1

I'm attempting to set a label in a macro(?) I created with a new command. I'm passing several values to the command in the first argument via pgfkeys, but the \label macro is just using the literal string I've put in to expand the pgfkey instead of the actual expanded value.

I've tried both of the solutions presented in these answers, together and separately, but not luck.

https://tex.stackexchange.com/a/308164

https://tex.stackexchange.com/a/125099

I've an inkling that what's happening is related to the timing of the expansion for the pgfkey value, but I'll be honest I don't have a firm understanding of how the underlying tex engine does it's magic. I'm also just learning pgfkeys so my understanding of what's happening there is still at a beginner level.

Here's my mwe of what I'm trying to do:

\documentclass[twocolumn]{book}

\usepackage[framemethod=TikZ]{mdframed}%boxes
\usepackage{pgfkeys}

\newcounter{myCounter}

\newmdenv[%
frametitlebackgroundcolor=blue,
frametitlefontcolor=white,
backgroundcolor=blue!25,
linecolor=blue,
outerlinewidth=1pt,
roundcorner=1mm,
skipabove=\baselineskip,
skipbelow=\baselineskip,
font=\small,
nobreak=true,
settings={\global\refstepcounter{myCounter}},
]{myTextBox}


%Define Macros
\makeatletter

\pgfkeys{/mykeys/textbox/.cd,
    title/.initial=,
    body/.initial=,
    label/.initial=,
}

\def\mykeys@set@textbox@keys#1{%%
    \pgfkeys{/mykeys/textbox/.cd,#1}}
\def\mykeys@get@textbox#1{%%
    \pgfkeysvalueof{/mykeys/textbox/#1}}

\newcommand\myBox[1]{%%
    \bgroup
    \mykeys@set@textbox@keys{#1}%%
    \begin{myTextBox}[frametitle=\textbf{\mykeys@get@textbox{title}\hfill NOTE}]
        \mykeys@get@textbox{body}
        \label{box:\mykeys@get@textbox{label}}
    \end{myTextBox}
    \egroup
}

\makeatother

\begin{document}
\section{A Section}
\myBox{
    title=Box Title,
    body=A not so long string of text to go in the box,
    label=firstbox
}
\ref{firstbox}

\end{document}

Here's my current result:

current result

I'm not tied to a particular solution (I would prefer a solution that works with pgfkeys) as long as I can get labels and references working properly.

Tuffwer
  • 200
  • Maybe I'm missing the obvious, but haven't you set the label to box:firstbox? \ref{box:firstbox} gives 1. But \ref{firstbox} is undefined because you've never created such a label. – David Purton Mar 14 '19 at 02:59
  • I... yep, had a type there in the reference, fixed that and it worked. Then followed this mwe and went through line by line on my actual document and it looks like I had made a similar issue with the reference value. I feel silly now. – Tuffwer Mar 14 '19 at 03:32
  • No worries! I have done the same on this site. :( I'll vote to close the question as off topic since it's basically just a typo. – David Purton Mar 14 '19 at 03:34
  • 4
    I'm voting to close this question as off-topic because the issue was caused by a typo. – David Purton Mar 14 '19 at 03:35
  • Sure thing, I can also just delete it myself, or do you think it's worth leaving but closed? – Tuffwer Mar 14 '19 at 03:38
  • 1
    Up to you, but the question demonstrates how to do something interesting which might be of use to others. – David Purton Mar 14 '19 at 03:39

0 Answers0