3

As shown in fig, if I use the keyval, then my MWE does not work well.

enter image description here

What's wrong with my MWE?

\documentclass{article}

\usepackage{keyval}
\usepackage{expl3}
\usepackage{xparse}

\makeatletter
\def\BUPTthesiscntitlepage#1{\setkeys{cntitle}{#1}}
\define@key{cntitle}{title}{\long\def\BUPT@value@title{#1}}
\makeatother

\makeatletter\ExplSyntaxOn
\seq_new:N \l_@@_title_seq
\NewDocumentCommand \settitle {m}
{ \seq_set_split:Nnn \l_@@_title_seq { \\ } {#1} }

\dim_new:N \l_@@_title_width_dim
\NewDocumentCommand \printtitle {}
{
    \group_begin:
    \@@_title_name_format:
    \tl_use:N \c_@@_title_name_tl
    \group_end:
    \group_begin:
    \@@_title_format:
    \@@_get_max_width:NN \l_@@_title_width_dim \l_@@_title_seq
    \dim_add:Nn \l_@@_title_width_dim { 10\p@ }
    \dim_log:N \l_@@_title_width_dim
    \parbox [t] { \l_@@_title_width_dim }
    {
        \seq_map_inline:Nn \l_@@_title_seq
        { \@@_underline:Nn \l_@@_title_width_dim {##1} \\ }
    }
    \group_end:
}

\cs_new_protected:Npn \@@_get_max_width:NN #1#2
{
    \group_begin:
    \dim_zero:N #1
    \bool_until_do:nn { \seq_if_empty_p:N #2 }
    {
        \seq_pop:NN #2 \l_tmpa_tl
        \exp_args:NNV \@@_get_width:Nn \l_tmpa_dim \l_tmpa_tl
        \dim_gset:Nn #1 { \dim_max:nn { \dim_use:N #1 } { \l_tmpa_dim } }
    }
    \group_end:
}
\cs_new_protected:Npn \@@_get_width:Nn #1#2
{
    \hbox_set:Nn \l_tmpa_box {#2}
    \dim_set:Nn #1 { \box_wd:N \l_tmpa_box }
}

\cs_new_protected:Npn \@@_underline:Nn #1#2
{ \underline { \hbox_to_wd:nn { \dim_use:N #1 } { \hss #2 \hss } } }

\tl_const:Nn \c_@@_title_name_tl    { Title: }
\cs_new:Npn  \@@_title_name_format: { \Large \bfseries }
\cs_new:Npn  \@@_title_format:      { \Large \bfseries }

\ExplSyntaxOff\makeatother

\BUPTthesiscntitlepage{%
    title = {AAAA \\ BBB\LaTeX B}}

\begin{document}
\settitle{AAAA \\ BBB\LaTeX BB}
\printtitle

\makeatletter
\long\def\BUPTtitletemp{\BUPT@value@title}
\makeatother

\settitle{\BUPTtitletemp}
\printtitle
\end{document}

  • @@ is not to be used as a prefix. – egreg Jun 2 '19 at 9:34
4

When you set the title like \settitle{\BUPTtitletemp}, the \BUPTtitletemp macro is not expanded. So the splitting at \\ fails. Expansion only happens when you actually typeset the lines in an \hbox (where you can't have a linebreak).

An easy fix in this case is to make sure the content that is passed to \settitle is at least expanded that far that an unexpandable token occurs at its start:

\exp_args_generate:n { Nnf }
\NewDocumentCommand \settitle {m}
{ \exp_args:NNnf \seq_set_split:Nnn \l_@@_title_seq { \\ } {#1} }

The result is as expected

enter image description here

|improve this answer|||||
  • Thanks for your answer! – DangeRS2 Jun 2 '19 at 4:36
6

First and foremost: @@ is an internal convention used in .dtx files to denote a common prefix in functions and variables and should not be used in production code.

The simplest ways to cope with your problem is to avoid keyval and use the more powerful features in expl3.

You can so store the title as a sequence, splitting it at \\, instead of having to do it later.

Also a tabular is easier than \parbox, as it doesn't require to measure the items.

\documentclass{article}

\usepackage{expl3}
\usepackage{xparse}

\ExplSyntaxOn

\keys_define:nn { danger/BUPT }
 {
  title .code:n = { \seq_set_split:Nnn \l_danger_title_seq { \\ } { #1 } },
 }
\seq_new:N \l_danger_title_seq

\NewDocumentCommand{\BUPTthesiscntitlepage}{m}
 {
  \keys_set:nn { danger/BUPT } { #1 }
 }


\NewDocumentCommand \printtitle {}
 {
  \group_begin:
  \danger_title_name_format:
  \tl_use:N \c_danger_title_name_tl
  \c_space_tl
  \group_end:
  \group_begin:
  \danger_title_format:
  \begin{tabular}[t]{@{} l @{}}
  \seq_map_function:NN \l_danger_title_seq \__danger_underline_endline:n 
  \end{tabular}
  \group_end:
 }

\cs_new_protected:Npn \__danger_underline_endline:n #1
 {
  \underline { #1 } \\
 }

\tl_const:Nn \c_danger_title_name_tl    { Title: }
\cs_new_protected:Npn  \danger_title_name_format: { \Large \bfseries }
\cs_new_protected:Npn  \danger_title_format:      { \Large \bfseries }

\ExplSyntaxOff

\BUPTthesiscntitlepage{
  title = {AAAA \\ BBB\LaTeX B},
}

\begin{document}

\printtitle

\end{document}

enter image description here

|improve this answer|||||
  • Thanks for your answer! – DangeRS2 Jun 3 '19 at 3:33
4

Note that \settitle is defined as

\seq_set_split:Nnn \l_@@_title_seq { \\ } {#1}

which meanse its argument should be a set of tokens given in braces. So

\settitle{AAAA \\ BBB\LaTeX BB}

will take AAAA \\ BBB\LaTeX BB as the argument, then split it at \\ and push AAAA, BBB\LaTeX BB into \l_@@_title_seq.

However, when using

\settitle{\BUPTtitletemp}

the argument is just \BUPTtitletemp (NOT its value), which has no \\ explicitly. Therefore, the whole title AAAA \\ BBB\LaTeX B will be pushed into \l_@@_title_seq as a single item. This will lead to errors since the following function such as \@@_get_max_width:NN does not allow \\ in its argument.

|improve this answer|||||
  • Thanks for your help! – DangeRS2 Jun 2 '19 at 4:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.