8

I’m trying to add PDF keywords to my résumé. In the custom class file, I have these lines:

\clist_new:N \jcsres_keywords_clist
\NewDocumentCommand \keywords {m} { \clist_gset:Nn \jcsres_keywords_clist {#1} }

\AtBeginDocument{
  \hypersetup{
    pdfinfo={
      Author={\jcsres_name_tl},
      Keywords={\jcsres_keywords_clist},
    }
  }
}

The \keywords command is intended to be used in the document preamble, like \author and \title in the standard classes. My hope was to initialize it with a standard comma-separated list, and pass the list more-or-less directly to \hypersetup.

The code above, though, seems (from looking at the PDF in Adobe Reader) to have a single keyword, of the form "keyword1,keyword2,"—not at all what I’m hoping for.

According to http://en.wikibooks.org/wiki/LaTeX/Hyperlinks#Customization, the syntax \hypersetup expects is

pdfkeywords={keyword1} {key2} {key3},

Is there a straightforward way to translate between Expl3’s clists and this syntax?

ETA: User g.cov suggested the use of hyperxmp; I may well use that package, but I’d like to learn how to do the transformation on my own using Expl3. In particular, please note that

Keywords = { \clist_use:Nnnn \jcsres_keywords_clist
               {\egroup \bgroup} {\egroup \bgroup} {\egroup \bgroup} }

did not work.

  • I think l3prop is more suitable for your situation. – Marco Daniel May 21 '13 at 5:02
  • @MarcoDaniel, perhaps as an Expl3 replacement for the entire pdfinfo; but for the Keywords list, a clist seems most apropos. – J. C. Salomon May 21 '13 at 14:23
  • @J.C.Salomon After seeing your edit, I've added a different version that seems to do what you're looking for. – egreg May 21 '13 at 17:26
4

enter image description here

The other PDF viewers (okular, evince) seem to show the list OK, but it seems that only hyperxmp helps to fix this for Adobe Reader.

\documentclass{article}
\makeatletter
\def\keywords#1{\def\k@yw@rds{#1}}
\AtBeginDocument{\hypersetup{pdfkeywords={\k@yw@rds}}}
\makeatother

\usepackage{hyperxmp}
\usepackage{hyperref}

\keywords{LaTeX update, clist item}

\begin{document}
Hello.
\end{document}
6

You have to use the comma list variable:

\documentclass{article}
\usepackage{xparse}

\ExplSyntaxOn
\clist_new:N \g_jcsres_keywords_clist
\NewDocumentCommand \keywords {m} { \clist_gset:Nn \g_jcsres_keywords_clist {#1} }

\AtBeginDocument
 {
  \hypersetup
   {
    pdfinfo=
    {
     Keywords={\clist_use:Nnnn \g_jcsres_keywords_clist {,~} {,~} {,~} },
    }
   }
 }
\ExplSyntaxOff
\makeatletter
\@onlypreamble\keywords % don't allow \keywords in the document
\makeatother


\usepackage{hyperref}

\keywords{LaTeX3, clist}

\begin{document}

Hello.

\end{document}

enter image description here

If you want to pass the list of keywords each enclosed in braces without commas in between, as it appears to be the best way for using them, here's the way:

\documentclass{article}
\usepackage{xparse}

\ExplSyntaxOn
\seq_new:N \g_jcsres_keywords_seq
\NewDocumentCommand \keywords {m}
 {
  \clist_map_inline:nn {#1}
   { \seq_gput_right:Nn \g_jcsres_keywords_seq { {##1} } }
 }

\AtBeginDocument
 {
  \hypersetup
   {
    pdfinfo=
    {
     Keywords={\seq_use:Nnnn \g_jcsres_keywords_seq {~} {~} {~} },
    }
   }
 }
\ExplSyntaxOff
\makeatletter
\@onlypreamble\keywords
\makeatother


\usepackage{hyperref}

\keywords{LaTeX3, clist}

\begin{document}

Hello.

\end{document}

Here's what I get now in Adobe Reader:

enter image description here

  • Another function we need to decide about moving to 'stable' :-) – Joseph Wright May 21 '13 at 9:21
  • @JosephWright Possibly with a "shorthand" \clist_use:Nn for \clist_use:Nnnn when the three n arguments are the same. And also for \seq_use:Nnnn: the case when you need the same token list to be inserted between all two items is perhaps more common than the one where you need to distinguish between "two items" and "more than two items". – egreg May 21 '13 at 9:30
  • I think we already have \seq_use:Nnnn or similar in the 'candidates'. I'll raise this with the rest of the team: I think it's a 'definite yes' for the stable part. – Joseph Wright May 21 '13 at 10:00
  • @JosephWright After all it's just \cs_new:Npn \seq_use:Nn #1 #2 { \seq_use:Nnnn #1 { #2 } { #2 } { #2 } } – egreg May 21 '13 at 10:03
  • While I agree that it is semantically better to use \clist_use:Nnnn here (and that we should move it to be stable), why would it make any difference (I can't test here)? The only thing this \clist_use:Nnnn does is to add spaces after each comma in the list. I would have expected an expansion issue to be at the origin of the OP's problem. – Bruno Le Floch May 21 '13 at 15:55

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.