1

How does the following print

Heime Borgia
405 Hilgard Ave
Los Angeles
California
90095
United States

Likku Cirillu 409 Hilgard Ave

rather than

Heime Borgia
405 Hilgard Ave
Los Angeles
California
90095
United States

Likku Cirillu 409 Hilgard Ave Los Angeles California 90095 United States

I want the second call to use the existing values from the first call for the variables not set by the second call.

The implementation is

\documentclass[a4paper,12pt]{article}

\ExplSyntaxOn

\keys_define:nn { adr_keys } { name .tl_set:N = \l_adr_name_tl, role .tl_set:N = \l_adr_role_tl, kmpn .tl_set:N = \l_adr_kmpn_tl, dwlg .tl_set:N = \l_adr_dwlg_tl, hmlt .tl_set:N = \l_adr_hmlt_tl, vltw .tl_set:N = \l_adr_vltw_tl, cbrg .tl_set:N = \l_adr_cbrg_tl, knty .tl_set:N = \l_adr_knty_tl, pstc .tl_set:N = \l_adr_pstc_tl, ktry .tl_set:N = \l_adr_ktry_tl, }

\cs_new_protected:Nn \adr_print:n { \keys_set:nn { adr_keys } { #1 }

\begin {tabular} { @{} l@{} }

  \tl_use:N \l_adr_name_tl \\

  \tl_if_blank:VF \l_adr_role_tl
    { \tl_use:N \l_adr_role_tl \\ }

  \tl_if_blank:VF \l_adr_kmpn_tl
    { \tl_use:N \l_adr_kmpn_tl \\ }

  \tl_if_blank:VF \l_adr_dwlg_tl
    { \tl_use:N \l_adr_dwlg_tl \\ }

  \tl_if_blank:VF \l_adr_hmlt_tl
    { \tl_use:N \l_adr_hmlt_tl \\ }

  \tl_if_blank:VF \l_adr_vltw_tl
    { \tl_use:N \l_adr_vltw_tl \\ }

  \tl_if_blank:VF \l_adr_cbrg_tl
    { \tl_use:N \l_adr_cbrg_tl \\ }

  \tl_if_blank:VF \l_adr_knty_tl
    { \tl_use:N \l_adr_knty_tl \\ }

  \tl_if_blank:VF \l_adr_pstc_tl
    { \tl_use:N \l_adr_pstc_tl \\ }

  \tl_if_blank:VF \l_adr_ktry_tl
    { \tl_use:N \l_adr_ktry_tl \\ }

\end {tabular}

}

\NewDocumentCommand {\adrLflush} { m } { \begin {flushleft} \adr_print:n { #1 } \end {flushleft} }

\ExplSyntaxOff

\begin{document}

\adrLflush { name={Heime Borgia}, dwlg={405 Hilgard Ave}, cbrg={Los Angeles}, knty={California}, pstc={90095}, ktry={United States}, }

\adrLflush { name={Likku Cirillu}, dwlg={409 Hilgard Ave}, }

\end{document}

Ragonese
  • 171
  • 4
  • 4
    you are making local assignments inside a flushleft environment. All environments restore all local assignments that is basic latex behaviour unrelated to any specifics of the code. use global (gset) assigments if you want to escape an environment – David Carlisle Mar 11 '24 at 23:40

1 Answers1

2

You are making local assignments so they are restored at \end{flushleft} you seem to want global assignments.

enter image description here

\documentclass[a4paper,12pt]{article}

\ExplSyntaxOn

\keys_define:nn { adr_keys } { name .tl_gset:N = \g_adr_name_tl, role .tl_gset:N = \g_adr_role_tl, kmpn .tl_gset:N = \g_adr_kmpn_tl, dwlg .tl_gset:N = \g_adr_dwlg_tl, hmlt .tl_gset:N = \g_adr_hmlt_tl, vltw .tl_gset:N = \g_adr_vltw_tl, cbrg .tl_gset:N = \g_adr_cbrg_tl, knty .tl_gset:N = \g_adr_knty_tl, pstc .tl_gset:N = \g_adr_pstc_tl, ktry .tl_gset:N = \g_adr_ktry_tl, }

\cs_new_protected:Nn \adr_print:n { \keys_set:nn { adr_keys } { #1 }

\begin {tabular} { @{} l@{} }

  \tl_use:N \g_adr_name_tl \\

  \tl_if_blank:VF \g_adr_role_tl
    { \tl_use:N \g_adr_role_tl \\ }

  \tl_if_blank:VF \g_adr_kmpn_tl
    { \tl_use:N \g_adr_kmpn_tl \\ }

  \tl_if_blank:VF \g_adr_dwlg_tl
    { \tl_use:N \g_adr_dwlg_tl \\ }

  \tl_if_blank:VF \g_adr_hmlt_tl
    { \tl_use:N \g_adr_hmlt_tl \\ }

  \tl_if_blank:VF \g_adr_vltw_tl
    { \tl_use:N \g_adr_vltw_tl \\ }

  \tl_if_blank:VF \g_adr_cbrg_tl
    { \tl_use:N \g_adr_cbrg_tl \\ }

  \tl_if_blank:VF \g_adr_knty_tl
    { \tl_use:N \g_adr_knty_tl \\ }

  \tl_if_blank:VF \g_adr_pstc_tl
    { \tl_use:N \g_adr_pstc_tl \\ }

  \tl_if_blank:VF \g_adr_ktry_tl
    { \tl_use:N \g_adr_ktry_tl \\ }

\end {tabular}

}

\NewDocumentCommand {\adrLflush} { m } { \begin {flushleft} \adr_print:n { #1 } \end {flushleft} }

\ExplSyntaxOff

\begin{document}

\adrLflush { name={Heime Borgia}, dwlg={405 Hilgard Ave}, cbrg={Los Angeles}, knty={California}, pstc={90095}, ktry={United States}, }

\adrLflush { name={Likku Cirillu}, dwlg={409 Hilgard Ave}, }

\end{document}

David Carlisle
  • 757,742
  • What would be a good strategy that allows both possibilities: global and local, with global as option. – Ragonese Mar 12 '24 at 11:00
  • a good strategy would be not to do that – David Carlisle Mar 12 '24 at 11:01
  • the overwhelmingly common case would be just to use local, and here re-structure your calls so the key setting is before the flush environment not in it – David Carlisle Mar 12 '24 at 11:02
  • Really. The functionality I am discussing is allowing existing values or resetting them when using \adrLflush. – Ragonese Mar 12 '24 at 11:03
  • So putting \keys_set:nn { adr_keys } { #1 } before the flush environment. – Ragonese Mar 12 '24 at 11:05
  • yes but you'd have to re-arrange a bit so you don't call it twice – David Carlisle Mar 12 '24 at 11:06
  • Quite Right. How would one handle using existing values and resetting them to empty ? ktry .initial:n = \l_adr_ktry_tl, would keep initial values. right ? What option would I have to add far refreshing the variables ? – Ragonese Mar 12 '24 at 11:15
  • we have already told you that 1000 times, just reset by \keys_set:nn { adr_keys } { ktry=} or \let \l_adr_ktry_tl\@empty or however you like. – David Carlisle Mar 12 '24 at 11:18
  • why do you keep asking about initial it does nothing about "resetting" anything, it is just equivalent to calling \keys_set:nn once to set a value after the definition. – David Carlisle Mar 12 '24 at 11:19
  • I would then need to use default. My current difficulty is when to use initial and default and when not to bother about them. Because I can always reset the variable values to empty. – Ragonese Mar 12 '24 at 11:51
  • 1
    no why would you have to use default???? just do as you have been told multiple times, you do not need to change the key declaration or use default or initial, neither of which are related to resetting the values, just set the keys again or directly defint the macros to empty as I showed in a comment above – David Carlisle Mar 12 '24 at 12:18
  • Ok. Have included a starred version. The unstarred version empties the variables, whereas the starred version uses existing values without emptying. – Ragonese Mar 12 '24 at 15:38
  • sounds reasonable @Ragonese – David Carlisle Mar 12 '24 at 15:40