1

I'm using htlatex to generate html files from LaTeX input. It's working fine. However, I'd like 2 versions of the html output file: one where the hyperlinks point to files on the hard drive for offline use and another version for online use (where the hyperlinks point to analogous links on the Internet).

I have over a hundred links, so it's cumbersome when making minor changes to do it with two separate raw LaTeX files. Is there a way to set a flag/switch which allows me choose which set of links individual calls to \href use?

Something along these lines:

$htlatex --someflag N filename.tex htm,0

where N=1 would select the offline links and N=2 would use the equivalent online links. If this is possible, what is the syntax? Also, how would these flag options be indicated in the raw LaTeX file?

3
  • could you please provide a minimal working example? do you specify these links by hand, using \href or \url commands, or they come from \ref etc?
    – michal.h21
    Jun 22, 2017 at 19:37
  • Thanks, @michal.h21. I use hyperref package. I have calls like this: The \href{somedirectory/report.pdf}{report} shows what I mean. I'd this option: The \href{https://www.report.com/report.pdf}{report} shows what I mean. I'd like to do this without having to recode (I have over a hundred links). I would like a single input LaTeX file that allows both options/versions. Do you know how to do this?
    – parminides
    Jun 23, 2017 at 22:05
  • Of course I'll have edit the LaTeX file to add the online links once. But I'm hoping there's a way I can include both online and offline links in a single LaTeX file and then specify which ones I want in the html output in htlatex (or by some other method).
    – parminides
    Jun 23, 2017 at 22:15

2 Answers 2

3

tex4ht supports configuration file, where you can provide custom definitions for various commands. In your place, I would use a macro that holds the base path for your \href commands. In the configuration file, it can be redefined to point to another directory, or to completely different web page.

Simple example:

\documentclass{article}
\usepackage{hyperref}
\newcommand\hrefroot{somedirectory/}
\begin{document}
Hello \href{\hrefroot report.pdf}{report}


\end{document}

Custom command \hrefroot is introduced. It holds the path to file and it must be used in all \href commands. The configuration file for tex4ht can look like this:

\Preamble{xhtml}

\ifOption{external}{\renewcommand\hrefroot{https://www.report.com/}}{\renewcommand\hrefroot{report/}}
\begin{document}
\EndPreamble

The important line is

\ifOption{external}{\renewcommand\hrefroot{https://www.report.com/}}{\renewcommand\hrefroot{report/}}

the rest is only required structure of the configuration file. The \ifOption command checks for option which can be specified on the command line. If this option is used, then the \hrefroot is redefined to point to external site, report directory is used otherwise.

Basic way to compile the document is the following:

make4ht -c configfilename.cfg filename.tex 

You can specify the external option using the following syntax:

make4ht -c configfilename.cfg filename.tex "external"

And here is the result for both options:

<!--l. 5--><p class="noindent" >Hello <a 
href="report/report.pdf" >report</a>
</p>

and for external:

<!--l. 5--><p class="noindent" >Hello <a 
href="https://www.report.com/report.pdf" >report</a>
</p>
3
  • Thank you for your help. That's a cool trick, but it will not solve my problem. The external files have a different directory tree structure (and sometimes different filenames). So it could be reports/report.pdf (offline) and report.com/reports/may/May15Report.pdf (online). It would be very difficult (impossible, really) for me to change that. I should have made that clear by giving a better example. Can I use something analogous to \ifOption within the LaTeX file? Then I could code in both versions of the link inside the LaTeX file and pick which one I want with "external" flag.
    – parminides
    Jun 24, 2017 at 17:36
  • @parminides could you please edit your question and add some simple example with such markup?
    – michal.h21
    Jun 24, 2017 at 18:17
  • No need. See my answer. Thanks for your help.
    – parminides
    Jun 24, 2017 at 19:02
0

Thanks to attention and suggestions of @michal.h21, I figured out how to do what I want.

\newcommand{\hrefoption}[2]{#1} % #1 = offline; #2 = online

Then,

The \href{\hrefoption{somedirectory/report.pdf}{https://www.report.com/reports/may/May15Report.pdf}}{report} shows what I mean.

This will use the first option and generate the offline version. If I want the online version, I change definition of \hrefoption to

\newcommand{\hrefoption}[2]{#2} % #1 = offline; #2 = online

so that it ignores the first argument (offline) and uses the online option.

Thank you so much for your time and attention, @michal.h21. I didn't use their suggestion per se, but they got me thinking about it the right way. Thanks.

Edit to add - It's not exactly what I requested (a flag that's set when running htlatex), but it does what I need with minimal effort (changing #1 to #2 in \hrefoption definition.

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.