Expandindo \thechapter imediatamente

Expandindo \thechapter imediatamente

Este é um desdobramento de outra pergunta que postei aqui.

\documentclass[8pt]{book}
\usepackage{etoolbox}
\usepackage{hyperref}

\newcommand{\mychapter}[1]{\chapter{#1} \hypertarget{chapter::\theHchapter}{}}
\newcommand{\refChapter}{\hyperlink{chapter::\theHchapter}{\thechapter}}

\newcommand{\addTimeline}[2]{%
  \ifcsname timeLine#1\endcsname
    \csappto{timeLine#1}{\unexpanded{\\& #2 & } \thechapter}%
  \else
    \csdef{timeLine#1}{\unexpanded{\\\textbf{#1} & #2 & } \thechapter}%
  \fi
}

\newcount\timelineCounter
\newcommand{\printTimeline}[2]{%
  \def\timelineData{}%
  \timelineCounter=#1\relax
  \loop \ifnum \timelineCounter<#2
    \edef\timelineData{\expandonce{\timelineData}\csuse{timeLine\the\timelineCounter}}%
    \advance \timelineCounter 1
  \repeat
\begin{tabular}{c p{7cm} c}
  \timelineData
\end{tabular}
}


\begin{document}

\mychapter{First}
 \addTimeline{4}{Event1}
 \addTimeline{19}{Event2}
 \addTimeline{2}{Event3}

\mychapter{Second} 
 \addTimeline{29}{Event4}
 \addTimeline{15}{Event5}
 \addTimeline{19}{Event6}

\mychapter{Timeline}
  \printTimeline{1}{30}


\end{document}

Eu esperava que ele imprimisse dois capítulos vazios seguidos por um Capítulo 3, chamado Linha do Tempo com

  2 Event3        1
  4 Event1        1
 15 Event5        2
 19 Event2        1
    Event6        2
 29 Event4        2

mas em vez disso, na terceira coluna recebo todos os "3". No cenário ideal eu usaria \refChaptero comando que criei, em vez do \thechapter, mas o primeiro não compila. Não tenho ideia de como expandir esse tipo de expressão. Qualquer orientação seria apreciada.

Responder1

Você tem que usar \cseapptoand \csedef, mas isso requer outro \unexpandedna parte que você não deseja expandir:

\documentclass{book}
\usepackage{etoolbox}
\usepackage{hyperref}

\newcommand{\mychapter}[1]{\chapter{#1} \hypertarget{chapter::\theHchapter}{}}
\newcommand{\refChapter}{\hyperlink{chapter::\theHchapter}{\thechapter}}

\newcommand{\addTimeline}[2]{%
  \ifcsname timeLine#1\endcsname
    \cseappto{timeLine#1}{\unexpanded{\unexpanded{\\& #2 & }} \thechapter}%
  \else
    \csedef{timeLine#1}{\unexpanded{\unexpanded{\\\textbf{#1} & #2 & }} \thechapter}%
  \fi
}

\newcount\timelineCounter
\newcommand{\printTimeline}[2]{%
  \def\timelineData{}%
  \timelineCounter=#1\relax
  \loop \ifnum \timelineCounter<#2
    \edef\timelineData{\expandonce{\timelineData}\csuse{timeLine\the\timelineCounter}}%
    \advance \timelineCounter 1
  \repeat
\begin{tabular}{c p{7cm} c}
  \timelineData
\end{tabular}
}


\begin{document}

\mychapter{First}
 \addTimeline{4}{Event1}
 \addTimeline{19}{Event2}
 \addTimeline{2}{Event3}

\mychapter{Second} 
 \addTimeline{29}{Event4}
 \addTimeline{15}{Event5}
 \addTimeline{19}{Event6}

\mychapter{Timeline}
  \printTimeline{1}{30}


\end{document}

insira a descrição da imagem aqui

O mesmo com expl3, mas com menos problemas de expansão, pois podemos expandir \theHchaptere \thechapterquando a macro principal está sendo chamada.

\documentclass{book}
\usepackage{xparse}
\usepackage{hyperref}

\ExplSyntaxOn
\cs_set_eq:NN \latexchapter \chapter

% modify \chapter to add a hypertarget
\RenewDocumentCommand{\chapter}{sO{#3}m}
 {
  \IfBooleanTF{#1}
   {
    \latexchapter*{#3}
   }
   {
    \latexchapter[#2]{\hypertarget{chapter::\theHchapter}{#3}}
   }
 }

\NewDocumentCommand{\addTimeline}{smm}
 { % here \theHchapter and \thechapter are expanded prior to doing the hard work
  \benedict_timeline_add:xxnn { \IfBooleanF{#1}{\theHchapter} } { \thechapter } { #2 } { #3 }
 }

\cs_new_protected:Nn \benedict_timeline_add:nnnn
 {
  \tl_if_exist:cTF { l_benedict_timeline_#3_tl }
   { % the header has already appeared
    \tl_put_right:cn { l_benedict_timeline_#3_tl } { & #4 }
   }
   { % the header is new
    \tl_new:c { l_benedict_timeline_#3_tl }
    \tl_put_right:cn { l_benedict_timeline_#3_tl } { \textbf{#3} & #4 }
   }
  % now add the reference; #3 is the header
  % #1 and #2 are \theHchapter and \thechapter (expanded)
  \benedict_timeline_add_ref:nnn { #3 } { #1 } { #2 }
 }
\cs_generate_variant:Nn \benedict_timeline_add:nnnn { xx }

\cs_new_protected:Nn \benedict_timeline_add_ref:nnn
 {
  \tl_if_empty:nTF { #2 } % \theHchapter
   {% modify here for the formatting
    \tl_put_right:cn { l_benedict_timeline_#1_tl } { & \S #3 \\ }
   }
   {
    \tl_put_right:cn { l_benedict_timeline_#1_tl } { & \S \hyperlink{chapter::#2}{#3} \\ }
   }
 }

\NewDocumentCommand{\printTimeline}{mm}
 {
  \tl_clear:N \l__benedict_timeline_body_tl
  \int_step_inline:nnnn { #1 } { 1 } { #2 }
   {
    \tl_if_exist:cT { l_benedict_timeline_##1_tl }
     {
      \tl_put_right:Nv \l__benedict_timeline_body_tl { l_benedict_timeline_##1_tl }
     }
   }
  \begin{tabular}{c p{7cm} c}
  \tl_use:N \l__benedict_timeline_body_tl
  \end{tabular}
 }

\tl_new:N \l__benedict_timeline_body_tl
\cs_generate_variant:Nn \tl_put_right:Nn { Nv }
\ExplSyntaxOff

\begin{document}

\chapter{First}

\addTimeline{4}{Event1}
\addTimeline{19}{Event2}
\addTimeline{2}{Event3}

\chapter{Second} 

\addTimeline{29}{Event4}
\addTimeline{15}{Event5}
\addTimeline{19}{Event6}

\chapter{Timeline}

\printTimeline{1}{30}

\end{document}

informação relacionada