4

I want to define a lot of keys in preamble and concatenate inside the document. Like this code:

\documentclass{article}
\usepackage{etoolbox}

%---------------------------------------------------------------------
% my cls files
\providetoggle{firstkey}
\settoggle{firstkey}{true}

\newcommand{\keys}[1]{\newcommand{\mykeys}{#1.}}
\newcommand{\key}{}

\renewcommand{\key}[1]{\iftoggle{firstkey}{#1}{; #1}\settoggle{firstkey}{false}}

%-----------------------------------------------------------------------

\keys{
    \key{aaaaa}
    \key{bbbbb}
    \key{ccccc}
}

\begin{document}

\mykeys

\end{document}

But, this code generate:

aaaaa ; bbbbb ; ccccc .

How can I remove this white spaces to and get:

aaaaa; bbbbb; ccccc.

7

Add \ignorespaces in the appropriate places.

\documentclass{article}
\usepackage{etoolbox}

%---------------------------------------------------------------------
% my cls files
\providetoggle{firstkey}
\settoggle{firstkey}{true}

\newcommand{\keys}[1]{\newcommand{\mykeys}{\ignorespaces#1.}}
\newcommand{\key}[1]{%
  \iftoggle{firstkey}{#1}{; #1}%
  \settoggle{firstkey}{false}%
  \ignorespaces
}

%-----------------------------------------------------------------------

\keys{
    \key{aaaaa}
    \key{bbbbb}
    \key{ccccc}
}

\begin{document}

X\mykeys X

\end{document}

The X characters are used to show that no spurious space gets inserted.

enter image description here

A different approach with expl3. The user inputs the keys with a more natural syntax and it's a job for expl3 to normalize it. In the example there is a trailing semicolon that would produce an empty key, but it can be easily checked for and removed.

\documentclass{article}
\usepackage{xparse}

%---------------------------------------------------------------------
% my cls files

\ExplSyntaxOn
\NewDocumentCommand{\keys}{m}
 {
  \seq_set_split:Nnn \l_tmpa_seq { ; } { #1 }
  \tl_if_blank:xT { \seq_item:Nn \l_tmpa_seq { -1 } }
   {% user has a trailing semicolon, remove the last (empty) item
    \seq_pop_right:NN \l_tmpa_seq \l_tmpa_tl
   }
  \tl_set:Nx \mykeys { \seq_use:Nn \l_tmpa_seq { ;~ } .}
 }
\prg_generate_conditional_variant:Nnn \tl_if_blank:n { x } { T }
\ExplSyntaxOff

%-----------------------------------------------------------------------

\keys{
  aaaaa;
  bbbbb;
  ccccc;
}

\begin{document}

X\mykeys X

\end{document}
5

Add a % after each key, since a new line in TeX is a space. See What is the use of percent signs (%) at the end of lines?

\documentclass{article}
\usepackage{etoolbox}

%---------------------------------------------------------------------
% my cls files
\providetoggle{firstkey}
\settoggle{firstkey}{true}

\newcommand{\keys}[1]{\newcommand{\mykeys}{#1.}}
\newcommand{\key}{}

\renewcommand{\key}[1]{\iftoggle{firstkey}{#1}{; #1}\settoggle{firstkey}{false}}

%-----------------------------------------------------------------------

\keys{
    \key{aaaaa}%
    \key{bbbbb}%
    \key{ccccc}%
}

\begin{document}

\mykeys

\end{document}

pic

  • Ok. But I want to put first piece in a cls file. And \keys command will be edit by user. How to avoid character % removal by the user? – Bismarck Gomes Apr 26 '18 at 16:14
  • See @egreg's answer. – Troy Apr 26 '18 at 16:38

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.