2

I have a pattern as follows

\href{https://tex.stackexchange.com/}{\verb|stackexchange.com $1|}

\verb|stackexchange.com $1|

the generated pdf is:

generated pdf

what I want is stackexchange.com $1. So what can I do?

Escape character \$ is obviously a solution, but I just want plain text so there is no need to add escape character when I use \newcommand for this pattern, like:

\newcommand{\tex}[2]{\href{https://tex.stackexchange.com/#2}{#1}}

and use it in the following way:

\tex{\verb|stackexchange.com $1|}{test}
3
  • Welcome to TeX.SE!
    – Mensch
    Sep 4, 2022 at 15:32
  • tex.stackexchange.com/questions/629134/… may be related.
    – Cicada
    Sep 4, 2022 at 16:01
  • You are at stake here: you can rather easily define a \verbhref command that absorbs the second argument verbatim, but then you need to escape special characters in the URL. The \href command works hard to be able to absorb the first argument without the need to escape special characters. Do you expect such URLs?
    – egreg
    Sep 4, 2022 at 16:08

1 Answer 1

2

Getting a working instance of the command \verb into \href's 2nd argument can be done by having stuff that goes into \href's 2nd argument read from .tex-input-file and tokenized under verbatim-category-code-régime and "then fed" to \scantokens under usual category-code-régime for re-tokenization as usual.

For retokenizing under usual category-code-régime you might like to have a % at the end of the line which then gets interpreted as a comment-character that prevents \scantokens' last inserted endline-character from being effective.

Instead of \tex the command is called \MyMacro in the following.

\MyMacro relies on its arguments being read and tokenized while verbatim-catcode-régime is in effect.

Therefore \MyMacro works out

  • when obtaining the tokens that form these arguments by reading and tokenizing from .tex-input-file.
  • when the tokens forming these arguments at some other moment were read and tokenized while verbatim-category-code-régime was in effect before passing them on to \MyMacro.

Therefore \MyMacro in many situations does not work out if the tokens forming its argument were read and tokenized under a category-code-régime differing from verbatim-category-code-régime, e.g., under usual category-code-régime.
E.g., \section{\MyMacro{\verb|stackexchange.com $1|}{test}} does not work out because the tokens forming the arguments of \MyMacro were tokenized under usual category-code-régime (instead of verbatim-category-code-régime) when the argument of \section was read from the .tex-file and tokenized.
Usage of \MyMacro within moving arguments or pdf-strings is generally a big problem.
(A "moving argument" in TeX-jargon is a macro argument which can wind up automatically in many places in the document. E.g., arguments of sectioning-commands like \chapter and \section are moving arguments because they can make it into the table of contents, into \nameref-cross-references, into page-headers, into pdf-bookmarks,...
A pdf-string, e.g., the text of a bookmark within the .pdf-file, is a component of the "infrastructure" of the .pdf-file itself and needs to be encoded in a way which complies to pdf-standards and thus can be understood by programs for viewing and/or otherwise processing .pdf-files.)

\documentclass{article}
\usepackage{hyperref}
\begingroup
\catcode`\Z=14 %
\catcode`\%=12 Z
\csname @firstofone\endcsname{Z
  \endgroup
  \newcommand\scantokenswithoutendline[1]{\begingroup\newlinechar=-1 \escapechar=92 \scantokens{\endgroup#1%}}Z
}%
\NewDocumentCommand{\MyMacro}{vv}{%
  \href{https://tex.stackexchange.com/#2}{\scantokenswithoutendline{#1}}%
}%

\begin{document}

% This creates a link to the not-existing page
%   https://tex.stackexchange.com/test .
% The phrase of the link is:  \verb|stackexchange.com $1|

\MyMacro{\verb|stackexchange.com $1|}{test}

\end{document}

enter image description here

1
  • Thanks a lot! I don't understand the code you show because there are some commands I have not learned or used yet, like scantokenswithoutendline, \catcode, \csname. But I have solved my problem. I guess they are commands to interpret the \verb code using verbatim-catcode-régime. Thanks very much!
    – Qingwei Li
    Sep 5, 2022 at 6:48

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .