1

Some users on the main Tex-chatroom of this site (one using TeX Live) have reported that this (or similar) code IS outputting clickable URLs for them, while for me (using TeXShop), it ISN'T ... What could be the problem?

MWE

\documentclass[a4paper]{article}
\usepackage{xparse, hyperref}

\ExplSyntaxOn
\cs_set_eq:NN
\IfEmptyTF
\tl_if_blank:nTF
\ExplSyntaxOff

\NewDocumentEnvironment {mainentry} { m m m } {%
  \IfEmptyTF{#3}{
First argument returns #1 NO
  }{%
Second argument returns #2 YES
  }%
  }{%
  \href{http://a.beautiful.url/?searchid=#1}{Click on me}%
}

\begin{document}

A) There should be a YES here. OK.

\begin{mainentry}{
first
}{
second
}{
third
}
\end{mainentry}

B) There should be a NO here. OK.

\begin{mainentry}{
first
}{
second
}{
%
}
\end{mainentry}

\end{document}
O0123
  • 1,773
  • There is still a space in the third arg in B right after the {, same with the others – daleif Oct 08 '17 at 06:07
  • @daleif Did you mean to write this with regards to the original question (OP), or with regards to my answer? – O0123 Oct 08 '17 at 06:17
  • Both, the % at the end does not help much when there is also an unwanted space at the start of the arg right after the { – daleif Oct 08 '17 at 09:32
  • @daleif But, as far as my answers go, I don't see any difference in compiling by adding even more %s, right? I believe there are no more compiling problems with the two sub-answers I proposed? – O0123 Oct 08 '17 at 09:38
  • Not at a pc so I cannot test. I'd like to know if there is a difference between if_blank from xparse and ifblank from etoolbox. The later should detect blank arg in case B. But what exactly is your issue here? Have you tested the pdf in something else than the pdf viewer from texshop? – daleif Oct 08 '17 at 09:42
  • @daleif I have checked different pdf viewers now and presented the results in my answer. – O0123 Oct 08 '17 at 11:46
  • So basically you need a macro to strip spaces at either end of an input. If you search the site, there should be methods. Perhaps the xstring package can help. – daleif Oct 08 '17 at 11:50
  • package xinttools provides macros for stripping spaces at either end of input. Documentation is as part of xint (texdoc xint and go to Macros of the xinttools package). –  Oct 09 '17 at 12:12

2 Answers2

2

The problem here are the spaces. Following User daleif's good suggestion (comment to OP) to check the PDF-output with different viewers, I analyzed the following, when opening the PDF-output from the OP (all on Mac):

  • TeXShop 3.88: no sign of any hyperlink
  • Safari 11.0: no sign of any hyperlink
  • Adobe Acrobat Reader DC 17.012.20098: outputs a clickable hyperlink, namely http://a.beautiful.url/?searchid=%20first%20 instead of http://a.beautiful.url/?searchid=first

Wherein %20 is the percent-encoding of a space.

One example which solves the problem is by changing the syntax layout of e.g.

Original

\begin{mainentry}{
first
}{
second
}{
third
}
\end{mainentry}

into

Solution 1

\begin{mainentry}
{first}
{second}
{third}
\end{mainentry}

Which I actually find very bothersome, because when copy-pasting manual lines (in this case, for example, of content "first", "second", "third"), it's much easier to paste them into empty lines (cf. Original) than to paste them between curly brackets (cf. Solution 1).

To solve at least half of the problem, then, one could propose Solution 2, where by inserting some instances of % we can at least keep one side of the blank lines empty before pasting anything into them:

Solution 2

\begin{mainentry}{%
first%
}{
second%
}{
third%
}
\end{mainentry}
O0123
  • 1,773
  • Solution three would be to put \tl_trim_spaces:n inside the argument so whatever the spaces they are removed. – Manuel Oct 08 '17 at 12:36
  • @Manuel How exactly to arrange all the syntax correctly? I would be interested in an answer following that strategy, if you could post one? – O0123 Oct 08 '17 at 13:39
1

Just in addition to Manuels comment

\ExplSyntaxOn
\cs_set_eq:NN\IfEmptyTF\tl_if_blank:nTF
\cs_set_eq:NN\Trim\tl_trim_spaces:n
\ExplSyntaxOff

\NewDocumentEnvironment {mainentry} { m m m } {%
  \IfEmptyTF{#3}{
    ... 3 is empty
  }{%
    ... 3 is not empty
  }%
  {%
    \typeout{'#1'}% just to verify
    \edef\temp{\Trim{#1}}
    \typeout{'\temp'}% just to verify
    \expandafter\href\expandafter{http://a.beautiful.url/?searchid=\temp}{Click
      on me}%
  }
}

The rest of the code is the same as in the OPs

daleif
  • 54,450