Advertisement
VincentK2000

LaTeX htlatex href using labels as ids

May 19th, 2021 (edited)
2,355
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Latex 5.35 KB | None | 0 0
  1. % About: https://tex.stackexchange.com/a/597771/242407
  2. \documentclass{article}
  3.  
  4. \makeatletter
  5. \expandafter\newif\csname ifishtml\endcsname
  6.  
  7. \@ifpackageloaded{tex4ht}{
  8.    \ishtmltrue
  9. }{
  10.    \ishtmlfalse
  11. }
  12.  
  13. \ifishtml
  14.     % BEGIN Source: https://tex.stackexchange.com/questions/42927/converting-from-latex-to-html-using-htlatex
  15.     \usepackage[dvips]{color,graphicx}
  16.    \usepackage[tex4ht]{hyperref}
  17.     % END
  18.  
  19.     \let\orig@section\section
  20.     \renewcommand\section[1]{%
  21.         \def\@sectiontitle{#1}%
  22.         \futurelet\@aftersectioncommand\resolvesection
  23.     }
  24.  
  25.     \def\writeSetHrefFallback{
  26.        \write\@auxout{\protect\ifx\protect\sethrefaccessible\protect\undefined
  27.            \protect\typeout{HTMLLabels: Fallback was used!}
  28.            \protect\def\protect\sethrefaccessible \string##1{}
  29.            \protect\fi}%
  30.     }
  31.  
  32.     \def\hrefaccessible{}
  33.  
  34.    \def\sethrefaccessible#1{
  35.        \expandafter\global\expandafter\let\csname hrefaccessible@#1\endcsname\hrefaccessible
  36.    }
  37.  
  38.     \def\resolvesection{%
  39.         \let\@storedlabel\relax
  40.         \ifx\@aftersectioncommand\label
  41.             \typeout{HTMLLabels: Section followed by label}%
  42.             \let\orig@label\label
  43.             \def\label##1{%
  44.                 \def\@storedlabel{##1}%
  45.                 \def\curhypid{##1}%
  46.                 \write\@auxout{\protect\sethrefaccessible{##1}}%
  47.                 \let\label\orig@label
  48.                 \expandafter\orig@section\expandafter{\@sectiontitle}%
  49.                 \label{##1}%
  50.             }%
  51.         \else
  52.             \let\curhypid\relax
  53.             \typeout{HTMLLabels: Section not followed by label}%
  54.             %
  55.             \expandafter\orig@section\expandafter{\@sectiontitle}%
  56.         \fi
  57.     }
  58.  
  59.     \let\orig@ref\ref
  60.     \newcommand\htmllabelref[1]{%
  61.         \typeout{HTMLLabels: For #1: \expandafter\meaning\csname hrefaccessible@#1\endcsname}
  62.        \expandafter\ifx\csname hrefaccessible@#1\endcsname\hrefaccessible
  63.            \begingroup
  64.            \def\rEfLiNK##1##2{##2}%
  65.             \Link{#1}{}%
  66.             \orig@ref{#1}%
  67.             \EndLink
  68.             \endgroup
  69.         \else
  70.             \orig@ref{#1}%
  71.         \fi
  72.     }
  73. \else
  74.     \usepackage[pdftex]{color,graphicx}
  75.    \usepackage[margin=2.5cm,a4paper]{geometry}
  76.    \usepackage{hyperref}
  77. \fi
  78.  
  79. \makeatother
  80.  
  81. \begin{document}
  82.  
  83. \makeatletter
  84. \ifishtml
  85.    \writeSetHrefFallback
  86.  
  87.    \LinkCommand\HeaderTarget{div,href,id}
  88.  
  89.    \def\curhypid{sec:unknown}
  90.  
  91.    \Configure{section}
  92.    {}{}  
  93.     {%
  94.     \HCode{<h3>}
  95.    \ifx\curhypid\relax
  96.        \PackageWarning{HTMLLabels}{Section misses label!}
  97.    \else
  98.        \HeaderTarget{}{\curhypid}\EndHeaderTarget
  99.    \fi
  100.    \TitleMark\space}
  101.    {\HCode{</h3>}}
  102.  
  103.    \ConfigureMark{section}{\thesection}
  104.    \def\ref{\htmllabelref}
  105.  
  106.     \let\orig@T@ref\T@ref
  107.     \def\alt@T@ref#1{%
  108.         \let\T@ref\orig@T@ref
  109.         \htmllabelref{#1}%
  110.         \let\T@ref\alt@T@ref
  111.     }
  112.     \let\T@ref\alt@T@ref
  113. \fi
  114. \makeatother
  115.  
  116. Lorem ipsum dolor sit amet, \ref{sec:further} (label does not follow section `Further heading' immediately:
  117. falls back to using the autogenerated id instead),
  118. consectetur adipiscing elit. Curabitur fringilla semper leo fringilla
  119. varius. Etiam nunc dolor, consequat elementum malesuada facilisis, rutrum ac massa. Maecenas et
  120. cursus libero. Maecenas tincidunt mattis hendrerit. Maecenas vitae accumsan est, id mattis mauris.
  121. Maecenas et massa sed tellus efficitur rutrum id a mauris. Etiam velit mi, pellentesque vitae
  122. tincidunt at, pharetra eget arcu. Nulla venenatis pulvinar dignissim.
  123.  
  124. Ut justo libero, pellentesque at fermentum mollis, tristique at tortor. Pellentesque ac nisi ligula.
  125. Quisque in lectus in dolor tempus pretium. Suspendisse placerat aliquam mauris, et porttitor tellus
  126. finibus id. Morbi ut nibh nisi.
  127.  
  128. Mauris fringilla bibendum dui eu vestibulum. Curabitur hendrerit, velit vitae pellentesque semper,
  129. sem neque molestie metus, ut tristique odio quam auctor enim. Nulla tortor mauris, posuere vel
  130. sagittis id, bibendum in nulla. Donec et sapien sit amet neque vestibulum fermentum. Maecenas lorem
  131. lectus, convallis ac ornare eu, viverra quis justo. Donec aliquam aliquet libero et lacinia. Nullam
  132. iaculis ligula interdum, imperdiet justo vitae, vehicula augue. Nam eu leo vitae dui gravida egestas
  133. ut sagittis purus. Aenean porta, nisi et tempus elementum, sapien ante sagittis arcu, non aliquam
  134. elit quam vestibulum felis. Phasellus et mauris facilisis, fringilla risus eu, iaculis dolor.
  135.  
  136. Fusce malesuada accumsan erat in tempor. Fusce in condimentum odio. Etiam quam sem, auctor vitae
  137. ante eu, laoreet pharetra metus. Duis eget libero quis diam malesuada venenatis. Duis sit amet nisl
  138. mauris.
  139.  
  140. \section{Section heading}\label{sec:intro}
  141.  
  142. Blah blah blah blah blah blah blah blah blah blah blah blah blah blah
  143. blah blah blah blah blah blah blah blah blah blah blah blah blah blah
  144. blah blah blah blah blah blah.  Here is a citation\,\cite{EXAMPLE}.
  145.  
  146. Blah blah blah blah blah blah blah blah blah blah blah blah blah blah
  147. blah blah blah blah blah blah blah blah blah blah blah blah blah blah
  148. blah blah blah blah blah blah.
  149.  
  150. This is a test to link to Section this text is for searching in HTML Karen heading
  151. \ref{sec:intro}.
  152.  
  153. % Don't do it like this! \label should follow \section{} immediately
  154. \section{Further heading}
  155.  
  156. \label{sec:further}
  157.  
  158. \end{document}
  159.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement