Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- % About: https://tex.stackexchange.com/a/597771/242407
- \documentclass{article}
- \makeatletter
- \expandafter\newif\csname ifishtml\endcsname
- \@ifpackageloaded{tex4ht}{
- \ishtmltrue
- }{
- \ishtmlfalse
- }
- \ifishtml
- % BEGIN Source: https://tex.stackexchange.com/questions/42927/converting-from-latex-to-html-using-htlatex
- \usepackage[dvips]{color,graphicx}
- \usepackage[tex4ht]{hyperref}
- % END
- \let\orig@section\section
- \renewcommand\section[1]{%
- \def\@sectiontitle{#1}%
- \futurelet\@aftersectioncommand\resolvesection
- }
- \def\writeSetHrefFallback{
- \write\@auxout{\protect\ifx\protect\sethrefaccessible\protect\undefined
- \protect\typeout{HTMLLabels: Fallback was used!}
- \protect\def\protect\sethrefaccessible \string##1{}
- \protect\fi}%
- }
- \def\hrefaccessible{}
- \def\sethrefaccessible#1{
- \expandafter\global\expandafter\let\csname hrefaccessible@#1\endcsname\hrefaccessible
- }
- \def\resolvesection{%
- \let\@storedlabel\relax
- \ifx\@aftersectioncommand\label
- \typeout{HTMLLabels: Section followed by label}%
- \let\orig@label\label
- \def\label##1{%
- \def\@storedlabel{##1}%
- \def\curhypid{##1}%
- \write\@auxout{\protect\sethrefaccessible{##1}}%
- \let\label\orig@label
- \expandafter\orig@section\expandafter{\@sectiontitle}%
- \label{##1}%
- }%
- \else
- \let\curhypid\relax
- \typeout{HTMLLabels: Section not followed by label}%
- %
- \expandafter\orig@section\expandafter{\@sectiontitle}%
- \fi
- }
- \let\orig@ref\ref
- \newcommand\htmllabelref[1]{%
- \typeout{HTMLLabels: For #1: \expandafter\meaning\csname hrefaccessible@#1\endcsname}
- \expandafter\ifx\csname hrefaccessible@#1\endcsname\hrefaccessible
- \begingroup
- \def\rEfLiNK##1##2{##2}%
- \Link{#1}{}%
- \orig@ref{#1}%
- \EndLink
- \endgroup
- \else
- \orig@ref{#1}%
- \fi
- }
- \else
- \usepackage[pdftex]{color,graphicx}
- \usepackage[margin=2.5cm,a4paper]{geometry}
- \usepackage{hyperref}
- \fi
- \makeatother
- \begin{document}
- \makeatletter
- \ifishtml
- \writeSetHrefFallback
- \LinkCommand\HeaderTarget{div,href,id}
- \def\curhypid{sec:unknown}
- \Configure{section}
- {}{}
- {%
- \HCode{<h3>}
- \ifx\curhypid\relax
- \PackageWarning{HTMLLabels}{Section misses label!}
- \else
- \HeaderTarget{}{\curhypid}\EndHeaderTarget
- \fi
- \TitleMark\space}
- {\HCode{</h3>}}
- \ConfigureMark{section}{\thesection}
- \def\ref{\htmllabelref}
- \let\orig@T@ref\T@ref
- \def\alt@T@ref#1{%
- \let\T@ref\orig@T@ref
- \htmllabelref{#1}%
- \let\T@ref\alt@T@ref
- }
- \let\T@ref\alt@T@ref
- \fi
- \makeatother
- Lorem ipsum dolor sit amet, \ref{sec:further} (label does not follow section `Further heading' immediately:
- falls back to using the autogenerated id instead),
- consectetur adipiscing elit. Curabitur fringilla semper leo fringilla
- varius. Etiam nunc dolor, consequat elementum malesuada facilisis, rutrum ac massa. Maecenas et
- cursus libero. Maecenas tincidunt mattis hendrerit. Maecenas vitae accumsan est, id mattis mauris.
- Maecenas et massa sed tellus efficitur rutrum id a mauris. Etiam velit mi, pellentesque vitae
- tincidunt at, pharetra eget arcu. Nulla venenatis pulvinar dignissim.
- Ut justo libero, pellentesque at fermentum mollis, tristique at tortor. Pellentesque ac nisi ligula.
- Quisque in lectus in dolor tempus pretium. Suspendisse placerat aliquam mauris, et porttitor tellus
- finibus id. Morbi ut nibh nisi.
- Mauris fringilla bibendum dui eu vestibulum. Curabitur hendrerit, velit vitae pellentesque semper,
- sem neque molestie metus, ut tristique odio quam auctor enim. Nulla tortor mauris, posuere vel
- sagittis id, bibendum in nulla. Donec et sapien sit amet neque vestibulum fermentum. Maecenas lorem
- lectus, convallis ac ornare eu, viverra quis justo. Donec aliquam aliquet libero et lacinia. Nullam
- iaculis ligula interdum, imperdiet justo vitae, vehicula augue. Nam eu leo vitae dui gravida egestas
- ut sagittis purus. Aenean porta, nisi et tempus elementum, sapien ante sagittis arcu, non aliquam
- elit quam vestibulum felis. Phasellus et mauris facilisis, fringilla risus eu, iaculis dolor.
- Fusce malesuada accumsan erat in tempor. Fusce in condimentum odio. Etiam quam sem, auctor vitae
- ante eu, laoreet pharetra metus. Duis eget libero quis diam malesuada venenatis. Duis sit amet nisl
- mauris.
- \section{Section heading}\label{sec:intro}
- Blah blah blah blah blah blah blah blah blah blah blah blah blah blah
- blah blah blah blah blah blah blah blah blah blah blah blah blah blah
- blah blah blah blah blah blah. Here is a citation\,\cite{EXAMPLE}.
- Blah blah blah blah blah blah blah blah blah blah blah blah blah blah
- blah blah blah blah blah blah blah blah blah blah blah blah blah blah
- blah blah blah blah blah blah.
- This is a test to link to Section this text is for searching in HTML Karen heading
- \ref{sec:intro}.
- % Don't do it like this! \label should follow \section{} immediately
- \section{Further heading}
- \label{sec:further}
- \end{document}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement