u-fischer / newpax

12 stars 3 forks source link

Feature request: information on name mapping #20

Closed werner-matthias closed 11 months ago

werner-matthias commented 1 year ago

Currently, newpax doesn't export the original names of link destinations but but renumbers them. It would be nice to get an information of the mapping between original and new name.

Background:

One application case could be common tables of contents for embedded pdfs, see https://tex.stackexchange.com/q/682625/12606.

briha commented 1 year ago

I am attempting the same thing, and hadn't seen your StackExchange post/this issue. I have managed to modify newpax so that it does export all the destinations, in addition to the ones it is just using for linking. I am taking the original name, prefixed by <filename>.newpax., so that no conflicts with destinations in the parent document occur. https://github.com/u-fischer/newpax/pull/22 makes that change. To have the destinations actually present in your file, they need to be requested with \NEWPAX@DestReq, where the first argument is the file.newpax and the second is the name of the destination (i.e., the number for the original ones, or the prefix + original destination name for all others).

I haven't got the table of contents working yet, however, so please let me know if you figure that out!

briha commented 1 year ago

With the current changes in #22, the following MWE works, including:

Still missing:

doc-use-newpax.tex

\DocumentMetadata{uncompress}
\documentclass{scrarticle}

\usepackage{pdfpages,xcolor}

\usepackage{hyperref}
\hypersetup{linkbordercolor=blue}
\usepackage{kantlipsum}

\usepackage{newpax}
\directlua
{
  require("newpax")
  newpax.writenewpax("doc-input")
}

\makeatletter

\def\@NEWPAX@TARGET@NAME#1{NEWPAX@\NEWPAX@file @#1@\csname l_@@_destsuffix_tl\endcsname}

\def\@contentsline@target@toc{toc}
\def\altcontentsline#1#2#3#4{
    \edef\@NEWPAX@PDFDEST@NAME{\@NEWPAX@TARGET@NAME{#4}}%
    \protected@write\@auxout{}{%
        \string\NEWPAX@DestReq{\NEWPAX@file}{#4}%
    }%
    \edef\addtocontentsX{{\string #1}{\string #2}{\string #3}{\@NEWPAX@PDFDEST@NAME}}%
    \addtocontents{\@contentsline@target}{\string\contentsline\addtocontentsX\protected@file@percent}%

    \ifx\@contentsline@target@toc\@contentsline@target%
      \edef\@itemlevel{\csname toclevel@#1\endcsname}% defined by hyperref
      \ifnum\@itemlevel>\c@tocdepth\else%
        \let\old@numberline\numberline%
        \let\numberline\@gobble%
        \expandafter\bookmark[dest=\@NEWPAX@PDFDEST@NAME, level=\@itemlevel]{#2}%
        \let\numberline\old@numberline%
      \fi
    \fi
}

% #1 = file name, #2 = target file
\def\IncludeIncludedFileMeta#1#2{%
    \def\NEWPAX@file{#1.newpax}%
    \def\@contentsline@target{#2}%
    \let\old@contentsline\contentsline%
    \let\old@contentsfinish\contentsfinish%
    \let\contentsline\altcontentsline%
    \let\contentsfinish\relax%
    \input{#1.#2}%
    \let\contentsline\old@contentsline%
    \let\contentsfinish\old@contentsfinish%
}

\gdef\@newlabel@parseaux@xx#1#2#3#4#5#6{
    \edef\@NEWPAX@PDFDEST@NAME{\@NEWPAX@TARGET@NAME{#5}}%
    \protected@write\@auxout{}{%
        \string\NEWPAX@DestReq{\NEWPAX@file}{#5}%
        \string\newlabel{\NEWPAX@ref@prefix#1}{{#2}{#3}{#4}{\@NEWPAX@PDFDEST@NAME}{#6}}%
    }%
}

\gdef\@newlabel@parseaux#1#2{\@newlabel@parseaux@xx{#1}#2}

\def\ParseAux#1{
  \makeatletter%
  \edef\NEWPAX@ref@prefix{#1:}%
  \def\NEWPAX@file{#1.newpax}%
%
  \let\old@writefile\@writefile%
  \let\old@BKM@entry\BKM@entry%
  \let\old@newlabel\newlabel%
%
  \let\@writefile\@gobbletwo%
  \let\BKM@entry\@gobble%
  \let\newlabel\@newlabel@parseaux%
%
  \input{#1.aux}%
%
  \let\newlabel\old@newlabel%
  \let\@writefile\old@writefile%
  \let\BKM@entry\old@BKM@entry%
  \makeatother%
}
\makeatother

\begin{document}
\tableofcontents
\listoffigures
\listoftables

\section{Main}
\kant[1-3]
\section{Next}
\kant[4]

\IncludeIncludedFileMeta{doc-input}{toc}
\IncludeIncludedFileMeta{doc-input}{lof}
\IncludeIncludedFileMeta{doc-input}{lot}
\ParseAux{doc-input}
\includepdf[pages=-]{doc-input}

\setcounter{section}{5}
\section{Last}
\begin{figure}
  \caption[main doc figure]{this is a figure in main doc}
\end{figure}
\kant[5-7]
\begin{table}
  \caption[main doc table]{this is a table in main doc}
\end{table}
This is a reference to an included section: \autoref{doc-input:2-1}

\end{document}

doc-input.tex

\DocumentMetadata{uncompress}
\documentclass{article}
\usepackage{hyperref}

\hypersetup{pdfborderstyle={/S/U/W 1},
   pdfauthor=Author,pdftitle=title,pdfcreator=creator}

\usepackage{kantlipsum}
\begin{document}

\makeatletter
% from \@starttoc, but without re-reading, creates toc file 
\newwrite\tf@toc\immediate\openout\tf@toc\jobname.toc\relax
\newwrite\tf@lof\immediate\openout\tf@lof\jobname.lof\relax
\newwrite\tf@lot\immediate\openout\tf@lot\jobname.lot\relax
\makeatother

\setcounter{section}{2} % to ensure correct section numbers
\setcounter{page}{3}    % ... and page numbers

\section{Section 3}\label{sec}
See Section~\ref{sec2}\par
\kant[3-6]
\section{Section 4}\label{sec2}
\begin{figure}
   \caption[input doc figure]{This is a figure in the input doc}
\end{figure}
\subsection{Section 4.1}\label{2-1}
\kant[8]
\section{Section 5}\label{sec3}
\begin{table}
   \caption[input doc table]{this is table in the input doc}
\end{table}
\kant[9]

\end{document} 

Edit: Now also supporting lists of tables and figures. Edit2: Added bookmark support, too. Edit3: Now also exporting the hyperref labels, allow to use \ref to internal labels (prefixed by <filename>:). Updated MWE to reflect that.

tobiasBora commented 1 year ago

Apparently, this issue might help to solve https://tex.stackexchange.com/questions/695277/clickable-includegraphics-for-cross-reference-data in order to cache latex code with references… As of right now I am not sure if the problem is from my original compilation (that displays a scary warning name{section.2} has been referenced but does not exist, replaced by a fixed one… maybe I should also export some macro and not just the content of the aux file?) or from newpax, but I’ll keep an eye on this issue.

briha commented 1 year ago

For it to work, you need to use the newpax version of https://github.com/u-fischer/newpax/pull/22, which is a patch to export every pdf destination.

Apparently, this issue might help to solve https://tex.stackexchange.com/questions/695277/clickable-includegraphics-for-cross-reference-data in order to cache latex code with references… As of right now I am not sure if the problem is from my original compilation (that displays a scary warning name{section.2} has been referenced but does not exist, replaced by a fixed one… maybe I should also export some macro and not just the content of the aux file?) or from newpax, but I’ll keep an eye on this issue. — Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you commented.Message ID: @.***>

tobiasBora commented 1 year ago

Awesome, I can confirm it works this way! Thanks a lot! Hope the PR will be merged soon.

u-fischer commented 1 year ago

I pushed a new version to CTAN which should resolve the issue. I have added an answer to the tex.sx question about how to use it

https://tex.stackexchange.com/a/695715/2388

I didn't use the PR as I wanted more control over the included destinations and less duplications.