I have a bibliography in which URLs are printed with a following "." I would like to omit the URLs to save space and I could do this by redefining the URL command, but this would leave me with a dot. Is there a way to define a command that eats a following "."?
Asked
Active
Viewed 699 times
8
2 Answers
16
The redefinition of
\urlis not trivial, because the argument might contain the percent character. Therefore the argument needs to be read with verbatim catcodes.The dot can be removed using
\@ifnextchar.\@gobble{}. This also catches the case that\url{...}is not followed by a dot. As side effect\@ifnextcharremoves spaces. This can be avoided by using\ltx@ifnextchar@nospaceof packageltxcmdsinstead of\@ifnextchar.
Example file:
\documentclass{article}
\usepackage{url}
\makeatletter
\newcommand*{\urlgobble}{%
\begingroup
% set verbatim catcodes
\let\do\@makeother
\dospecials
% restore catcode of argument braces
\catcode`\{=1 %
\catcode`\}=2 %
\@urlgobble
}
\newcommand*{\@urlgobble}[1]{%
% eats the URL in #1
\endgroup
% skip an optional dot
\@ifnextchar.\@gobble{}%
}
\newcommand*{\skipurls}{\let\url\urlgobble}
\makeatother
\begin{document}
Normal url:
\url{http://www.example.org/Hello%20World.html}
\skipurls
Skipped urls:
\url{http://www.example.net/Hello%20World.html}
\url{http://www.example.com/Hello%20World.html}.
\end{document}
egreg
- 1,121,712
Heiko Oberdiek
- 271,626
-
Great answer (as usual); I've changed
\urlgobbleinto\urlin the final example, which seemed to be your intent, as\skipurlwas given. – egreg Nov 14 '12 at 10:31 -
@egreg Thanks, it was a leftover from testing that I have forgotten to revert. – Heiko Oberdiek Nov 14 '12 at 10:47
5
Works only if the URL has no special characters!
\documentclass{article}
\usepackage{url}
\begin{document}
\url{http://www.dante.de}.
\makeatletter
\let\URL\url
\def\url#1{\@ifnextchar.\url@i{\url@i.}}
\def\url@i.{}
\makeatother
Not printed: \url{http://www.dante.de}.
\URL{http://www.dante.de}
\end{document}


\url{...}., then redefine\urlwith two arguments to do nothing with them. Otherwise something slightly more complicated should be done. – egreg Nov 14 '12 at 09:28urlfields? That would look like a more natural way of doing it, for example in case you have urls elsewhere in your text. – T. Verron Nov 14 '12 at 09:35\url{ ... }.I cannot influence this. So I have to redifine the url command to eat this dot. – Stefan Müller Nov 14 '12 at 09:37