Is there a way to define starred variants for a keyvalue system?
I don't know about PGF keys, but my idea would be to test whether the values are empty or not:
\documentclass{article}
\usepackage{xparse}
\NewDocumentCommand{\attrib}{m}{%
\nopagebreak{\par\raggedleft\footnotesize #1\par}}
\ExplSyntaxOn
\keys_define:nn { ae/epigraph }
{
title .tl_set:N = \l_ae_epigraph_title_tl,
title .initial:n = {},
description .tl_set:N = \l_ae_epigraph_description_tl,
description .initial:n = {},
author .tl_set:N = \l_ae_epigraph_author_tl,
author .initial:n = {},
longestline .code:n = \ae_settowidth:n { #1 },
afterskip .skip_set:N = \l_ae_epigraph_afterskip_skip,
afterskip .initial:n = 1\baselineskip,
}
\dim_new:N \l_ae_epigraph_longestline_dim
\dim_set:Nn \l_ae_epigraph_longestline_dim { 3in }
\cs_new_protected:Npn \ae_settowidth:n #1
{
\hbox_set:Nn \l_tmpa_box { #1 }
\dim_set:Nn \l_ae_epigraph_longestline_dim { \box_wd:N \l_tmpa_box }
}
\NewDocumentEnvironment{epigraph}{O{}}
{
\keys_set:nn { ae/epigraph } { #1 }
\par\hspace*{\fill}
\begin{minipage}[t]{\dim_eval:n { \l_ae_epigraph_longestline_dim+1em } }
}
{
\par
\group_begin:
\raggedleft\footnotesize
\tl_if_empty:NF \l_ae_epigraph_title_tl
{ \l_ae_epigraph_title_tl \\ }
\tl_if_empty:NF \l_ae_epigraph_description_tl
{ \l_ae_epigraph_description_tl \\ }
\tl_if_empty:NF \l_ae_epigraph_author_tl
{ \l_ae_epigraph_author_tl \\ }
\vspace{-\prevdepth}
\group_end:
\vspace{1ex}
\hrule
\end{minipage}%
\hspace*{\fill}%
\vspace{\l_ae_epigraph_afterskip_skip}
}
\ExplSyntaxOff
\pagestyle{empty}
\begin{document}
\begin{epigraph}[
title={title},
description={description},
author={me},
longestline=abcdefghijklm
]
Scenario 1
\end{epigraph}
\begin{epigraph}[
author={me},
longestline=abcdefghijklm
]
Scenario 2
\end{epigraph}
\begin{epigraph}[
title={title},
description={description},
longestline=abcdefghijklm
]
Scenario 3
\end{epigraph}
\begin{epigraph}[
title={title},
description={description},
longestline=abcdefghijklm
]
Scenario 4
\end{epigraph}
\begin{epigraph}[
title={title},
longestline=abcdefghijklm
]
Scenario 5
\end{epigraph}
\begin{epigraph}[
description={description},
longestline=abcdefghijklm
]
Scenario 6
\end{epigraph}
\end{document}
I'm using actual value-keys. These are setup with the .initial
handler (every value has an initial value). This reduces overhead, in my opinion. As it just a wrapper for a \csname
solution. A key of the path /tree/sub/name
is stored in the macro \pgfk@/tree/sub/name
. I don't see much of the reason why I'd define a key that simply sets another family of macros.
The macros starting with \@epi@…
in the environment are merely temporarily macros since they are only defined in the environment (which groups its content).
Code
\documentclass{article}
\newcommand{\attrib}[1]{\nopagebreak{\par\raggedleft\footnotesize #1\par}}
%%----------
\makeatletter
%%----------
\newlength{\widthofwidestline}
\newlength{\afterEpigraphSkip}
\setlength{\widthofwidestline}{3in}
%%----------
\RequirePackage{pgfkeys}
\def\epigraphset{\pgfqkeys{/ae/my/epigraph/environment/set/options}}
\def\epigraphgetvalue#1{\pgfkeysgetvalue{/ae/my/epigraph/environment/set/options/#1}}
\epigraphset{
title/.initial =,
description/.initial=,
author/.initial =,
longest line/.code = {\settowidth{\widthofwidestline}{#1}},
afterskip/.code = {\setlength{\afterEpigraphSkip}{#1}},
afterskip=1\baselineskip
}
\newenvironment{epigraph}[1]{
\epigraphset{#1}%
\par\hspace*{\fill}%
\begin{minipage}[t]{\dimexpr\widthofwidestline+1em}%
}{
\attrib{%
\epigraphgetvalue{title}\@epi@title
\epigraphgetvalue{description}\@epi@desc
\epigraphgetvalue{author}\@epi@author
% \noindent % needed (?) if you use \\
\@epi@title
\ifx\@epi@desc\pgfkeys@empty\else
\par\@epi@desc
\fi
\ifx\@epi@author\pgfkeys@empty\else
\par\@epi@author
\fi
\makebox[0pt][r]{\rule[-0.8pt]{1in}{0.4pt}}%
}%
\end{minipage}%
\hspace*{\fill}%
\vspace{\afterEpigraphSkip}%
}
\makeatother
\pagestyle{empty}
\begin{document}
\epigraphset{longest line=abcdefghijklm}
\begin{epigraph}{title=title,description=description,author=me}
TDA
\end{epigraph}
\begin{epigraph}{author=me}
\_\_A
\end{epigraph}
\begin{epigraph}{description=description}
\_D\_
\end{epigraph}
\begin{epigraph}{title=title}
T\_\_
\end{epigraph}
\begin{epigraph}{title=title,description=description}
TD\_
\end{epigraph}
\begin{epigraph}{title=title,author=author}
T\_A
\end{epigraph}
\begin{epigraph}{description=description,author=author}
\_DA
\end{epigraph}
\end{document}