3

I wasn't even sure how to formulate this question, nor the appropriate tags, so there may be some editing or even deletion if this is a stupid question... But I think it is a feature that people would want...


Often I find myself writing informative (I hope) documents that need \href{addr}{name} to provide more content. I know I could do it once, but then the reader has to hunt for the word among many pages to "go there".

It would be nice if I could just rely on a list of them, like in a .bib file, that a new definition could just provide the same functionality.

Silly example... Instead of this;

I like the search engine \href{https://duckduckgo.com}{duckduckgo} because \href{https://duckduckgo.com}{duckduckgo} provides me with privacy I don't really understand.

something like this;

I like the search engine \hrefcite{DDGo} because \hrefcite{DDGo} provides me with privacy I don't really understand.

(where there are a list of these items, e.g. `\newcommand{\DDGo}[1]{\href{https://duckduckgo.com}{DuckDuckGo}}`)

Maybe there is something like this already... I've used glossary before but I don't see how that helps either.


Currently, what sort of works is having the external file, /home/me/somewhere/hrefListForInput.tex under \input in the preamble of my main file, main.tex, e.g. \input{/home/me/somewhere/hrefListForInput}. In hrefListForInput.tex are things like

\newcommand{\DDGo}[0]{ \href{https://duckduckgo.com}{DuckDuckGo } }%
\newcommand{\foam}[0]{\href{https://sourceforge.net/projects/foam-extend/}{OpenFOAM }}

and in main.tex, I use these definitions like,

I like using \foam in \DDGo.

The 'sort of' problem manifests because if I don't put a space after the last letter in the 2nd argument of \href, it runs into the next word. If I do put a space, like I've written, it is fine except at ends of sentences where there is a weird space before the last word and the period.

Earlier I had an error whereby I needed to append the file extension, .tex to hrefListForInput.tex.

I'm using LuaLatex/Linux/TeXLive, an article document class, and so could use \directlua or things like that but a more universal approach would be better.

  • 1
    Oh, my bad... I guess it needed a .tex file extension... Now it works. But is there still a better method? Or is this it? – nate Jan 9 at 1:48
  • how about glossaries? can it present a hyper ref in its \gls command? – Elad Den Jan 9 at 10:18
  • I'm no expert, but on here, en.wikibooks.org/wiki/LaTeX/Glossary I couldn't find a way for any of the gls stuff to do anything but print out text.... – nate Jan 9 at 20:15
2

You can use biblatex. The easiest might be to define your own cite command

\documentclass{article}
\usepackage{biblatex}
\usepackage{hyperref}

\DeclareCiteCommand{\hrefcite}{\usebibmacro{prenote}}{%
    \usebibmacro{citeindex}%
    \href{\thefield{url}}{\printfield{label}}%
}{\multicitedelim}{\usebibmacro{postnote}}

\begin{filecontents}{\jobname.bib}
@online{foam,
  url          = {https://sourceforge.net/projects/foam-extend/},
  label        = {OpenFOAM},
}
@online{DDGo,
  url          = {https://duckduckgo.com},
  label        = {DuckDuckGo},
}
\end{filecontents}

\addbibresource{\jobname.bib}

\begin{document}
  I like using \hrefcite{foam} in \hrefcite{DDGo}.
\end{document}
1

You can add your urls to a bib file and then use biblatex. If needed you can define more cite commands (I once made one which inserted also the qr-code for such an url).

\documentclass{article}
\usepackage{biblatex}

\addbibresource{biblatex-examples.bib}

\begin{document}
\citeurl{ctan} 

\end{document}

enter image description here

  • I discoverd my above method doesn't add a space between it and the next word - great though at end of sentence. So looked at ctan.cs.uu.nl/macros/latex/contrib/biblatex/doc/biblatex.pdf and tried many different 'cites' but didn't see anything like \href{address}{print this}... Is there something in biblatex that could do that? – nate Jan 9 at 19:53
1

You can use glossaries to store and print your links:

\documentclass{article}

\begin{filecontents}{allLinks.tex}
\newglossaryentry{LINK1}{
    name={\href{https://www.overleaf.com/}{Linky link}},
    description = {\url{https://www.overleaf.com/}}
}

\newglossaryentry{LINK2}{
    name={\href{https://www.sharelatex.com/}{Link linky}},
    description = {\url{https://www.sharelatex.com}}
}
\end{filecontents}




\usepackage{hyperref}
\usepackage{glossaries}
\input{allLinks.tex}

\makeglossaries


\begin{document}
\section{One}
Texidy texti text \gls{LINK1} text text
\section{Two}
and also \gls{LINK2}

\printglossaries

\end{document}

This example is of a file with two links, you can change the way the list is printed using all the styles available for the glossaries package.

Using this MWE the links will be displayed as plain text. If you want them to be formatted as links take a look at https://tex.stackexchange.com/a/392724/90297

  • That's really nice! If it wasn't for the extra overhead - I sometimes don't need glossary but always need biblatex. Love the backref too. I see now about putting \href inside, here the name field of a glossary entry... – nate Jan 10 at 16:58

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.