Changes

Jump to navigation Jump to search
1,478 bytes added ,  19:33, 5 October 2023
adapt display to the wiki
URLs can be typeset in various ways. You may specify a URL for later reuse via {{cmd|useURL}}:
<texcode>
\useURL[aurl] [http://xkcd.com/149/] [] [I prefer hot dogs.]
</texcode>
where #1 is the identifier, #2 is the URL you want to point to, and #4 the text to be displayed where the URL is used. #3 can be used for the 'file path' portion of the URL, if you want. This dates back to when hyperlink support in
</texcode>
<!--You can use<texcode>\url[aurl]</texcode>as well, which behaves like {{cmd|from}}[#1].--> URL shortcuts defined by {{cmd|useURL}} can be used while {{cmd|url}} as well:
<texcode>
\url[aurl]
</texcode>
as well, which behaves like {{cmd||from}}[#1].
== How to Typeset URLs ==
=== Example for Mailto #1 ===
<context source=yes>
\setupinteraction[state=start]
\useurl [mymail] [mailto:nobody@example.zzz] [][visible@mailaddress.zzz]
\starttext
 
Send your spam to \from[mymail]!
Send your spam to \stoptext from[mymail]!
</context>
<context source=yes>
\setupinteraction[state=start] \starttext Send your spam to \goto{visible@mailaddress.zzz}[url(mailto:nobody@example.zzz)] \stoptext
</context>
=== Escaping of Special Characters ===
 
You might be interested in [[URL#Avoid_Escaping_Characters|not escaping characters]].
Note that special characters, eg hash sign, percent sign, must be escaped by backslash.
<context source=yes>
\setupinteraction[state=start] \useurl [myurl] [www.example.zzz/\#999] [][www.example.zzz/\#999]
</context>
ConTeXt provides a dedicated mechanism for chopping them into pieces:
<context source=yes>
\enabledirectives[hyphenators.urls.packslashes] % this avoids consecutive slashes each in a different line
\sethyphenatedurlbefore{/} % but then they seem to be fixed to what comes before and after them
\startTEXpage[width=7em, foregroundstyle=\ttb] % just to display the output better
\hsize\zeropoint % to see where lines may be broken
\hyphenatedurl{http://bits.blogs.nytimes.com/2010/03/05/a-former-book-designer-says-good-riddance-to-print/?hpw}
\stopTEXpage
</context>
 
The previous sample shows all possible line breaks, since horizontal size (text width) has been set to zero ({{cmd|hsize}}{{cmd|zeropoint}}).
=== Hyphenation Rules ===
These special commands can be used in other cases, like {{cmd|useURL}}, too.
 
== Setting Color and Style ==
== Tips and Tricks ==
 
=== Using variable URLs ===
When using variables, or macros in general, it's necessary to consider that <code>\useURL</code> '''doesn't expand''', which means that any commands within will be treated like text and printed plainly, i.e. instead of the hyperlink the command name will be pasted to the browser or mail client. To circumvent this use <code>\expanded{}</code> around <code>\useURL</code>:
 
<texcode>
\expanded{\useURL[][][][]}
</texcode>
 
The following MWE demonstrates the issue.
 
<texcode>
\setupinteraction[state=start]
%
\setvariables[Sender][Mail={ntg-context@ntg.nl}]
%
\useURL[Direct][mailto:ntg-context@ntg.nl][][ntg-context@ntg.nl]
\useURL[Unexpanded][mailto:\getvariable{Sender}{Mail}][][\getvariable{Sender}{Mail}]
% same as above but within \expanded{}:
\expanded{\useURL[Expanded][mailto:\getvariable{Sender}{Mail}][][\getvariable{Sender}{Mail}]}
%
\starttext
\from[Direct] Direct input works\blank
\from[Unexpanded] Unexpanded fails\blank
\from[Expanded] Expanded works\blank
%
In the PDF mind the tooltips and how the wrong line transfers to the mail client.
\stoptext
</texcode>
=== Many Different Hyperrefs ===
If you need many hyperrefs in your document these definitions may be handy for you
<context source=yes>
\def\href#1#2{\useURL[#2][{#2}][][{#1}]\goto{\url[#2]}[url(#1)]} \def\ahref#1{\color[linkcolor]{\ttx \href{#1}{<#1>}}} \def\fullahref#1{\color[linkcolor]{\ttx \href{#1}{http://#1}}} \def\mailto#1{\useURL[#1][mailto:#1][][#1]\from[{#1}]} \def\MailTo#1#2{\useURL[#1][mailto:#1][][#2]\from[{#1}]}
Send mail to \MailTo{foo@bar.zzz}{Mr. Foo}
</context>
 
=== Avoid Escaping Characters ===
 
In case you don’t want to escape characters from your URLs, you might want to use the following command:
 
<context source="yes">
\setupinteraction[state=start]
\setupnote[footnote][location=text]
\setupnotation[footnote][alternative=serried, numbercommand=\tfx]
\unexpanded\def\mypersonalurl#1{%
\bgroup\tt\goto{\hyphenatedurl{#1}}[url(#1)]\egroup}
 
\startasciimode
\mypersonalurl{http://a.b?hpw%20h#pw}
\stopasciimode
\startasciimode
\footnote{\mypersonalurl{http://a.b?hpw%20h#pw}}
\stopasciimode
\placenotes[footnote]
</context>
 
But you have to consider one option to make it work:
 
* Setting {{cmd|asciimode}} for the whole document.<p>Comments won’t work, because <code>%</code> will be considered a standard character.</p>
 
* Wrapping all URLs inside {{cmd|startasciimode}}...{{cmd|stopasciimode}}.<p>If you use URLs inside footnotes, it is the whole footnote which has to be inside the pair of commands.</p><p>Comments inside the mode won’t work.</p>
=== External Links ===
Create a macro for external links in a colour that is distinct from other document links.
<precontext source="yes">
% Enable document colours
\setupcolors[state=start,]
\def\Google{G\color[red]{o}\color[orange]{o}g\color[green]{l}\color[red]{e}}
%\starttext % makes no sense in the wiki\startTEXpage[offset=1dk, foregroundstyle=\tfb]
\completecontent
% \startchapter[title=chapter]
Search \index{source code}{source code} using \href{\Google{} Website}{http://google.com}! Read about
\index{syntax}{syntax}.
% \stopchapter \blank
\placeindex
\stopTEXpage%\stoptext% makes no sense in the wiki </precontext[[Category:Basics]][[Category:Interaction]]
139

edits

Navigation menu