help with \newcommand

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

help with \newcommand

Ramón Figueroa-Centeno
Aloha,

I want to define a new command that will make my life a lot easier. Here is what I would like:

\newcommand{\foo}[1]{#1 $\#1$}

So that I could invoke it as "\foo{aleph}" I get the same as if I typed "aleph $\aleph$" (no backslash on the first character).

Also, much harder (I think):

a command that would do URL encoding right from within LaTeX to pass arguments to the hyperref package:

\newcommand{\foo2}[1]{\href{<#1 url encoded>}{#1}}

so that "\foo2{Hello you}" would yield the same as typing "\href{http://Hello%20you}{Hello you}"

Thanks,

Ramón
Reply | Threaded
Open this post in threaded view
|

Re: help with \newcommand

Herbert Schulz

On Jun 5, 2010, at 9:10 AM, Ramón Figueroa-Centeno wrote:

>
> Aloha,
>
> I want to define a new command that will make my life a lot easier. Here is
> what I would like:
>
> \newcommand{\foo}[1]{#1 $\#1$}
>
> So that I could invoke it as "\foo{aleph}" I get the same as if I typed
> "aleph $\aleph$" (no backslash on the first character).
>
> Also, much harder (I think):
>
> a command that would do URL encoding right from within LaTeX to pass
> arguments to the hyperref package:
>
> \newcommand{\foo2}[1]{\href{<#1 url encoded>}{#1}}
>
> so that "\foo2{Hello you}" would yield the same as typing
> "\href{http://Hello%20you}{Hello you}"
>
> Thanks,
>
> Ramón

Howdy,

Try newcommand{\foo}[1]{#1 $\csname #1\endcsname$}. Not sure it's will work but...

Good Luck,

Herb Schulz
(herbs at wideopenwest dot com)



----------- Please Consult the Following Before Posting -----------
TeX FAQ: http://www.tex.ac.uk/faq
List Reminders and Etiquette: http://email.esm.psu.edu/mac-tex/
List Archive: http://tug.org/pipermail/macostex-archives/
TeX on Mac OS X Website: http://mactex-wiki.tug.org/
List Info: http://email.esm.psu.edu/mailman/listinfo/macosx-tex

Reply | Threaded
Open this post in threaded view
|

RE: help with \newcommand

Yan Zhou
In reply to this post by Ramón Figueroa-Centeno
The first problem can be solved using \csname#1\endcsname

i have little idea about the second as i don't know how to "encode" url


-----Original Message-----
From: Ramón Figueroa-Centeno
Sent:  05/06/2010 15:10:39
Subject:  [OS X TeX] help with \newcommand


Aloha,

I want to define a new command that will make my life a lot easier. Here is
what I would like:

\newcommand{\foo}[1]{#1 $\#1$}

So that I could invoke it as "\foo{aleph}" I get the same as if I typed
"aleph $\aleph$" (no backslash on the first character).

Also, much harder (I think):

a command that would do URL encoding right from within LaTeX to pass
arguments to the hyperref package:

\newcommand{\foo2}[1]{\href{<#1 url encoded>}{#1}}

so that "\foo2{Hello you}" would yield the same as typing
"\href{http://Hello%20you}{Hello you}"

Thanks,

Ramón
--
View this message in context: http://macosx-tex.576846.n2.nabble.com/help-with-newcommand-tp5142889p5142889.html
Sent from the MacOSX-TeX mailing list archive at Nabble.com.
----------- Please Consult the Following Before Posting -----------
TeX FAQ: http://www.tex.ac.uk/faq
List Reminders and Etiquette: http://email.esm.psu.edu/mac-tex/
List Archive: http://tug.org/pipermail/macostex-archives/
TeX on Mac OS X Website: http://mactex-wiki.tug.org/
List Info: http://email.esm.psu.edu/mailman/listinfo/macosx-tex



----------- Please Consult the Following Before Posting -----------
TeX FAQ: http://www.tex.ac.uk/faq
List Reminders and Etiquette: http://email.esm.psu.edu/mac-tex/
List Archive: http://tug.org/pipermail/macostex-archives/
TeX on Mac OS X Website: http://mactex-wiki.tug.org/
List Info: http://email.esm.psu.edu/mailman/listinfo/macosx-tex

Reply | Threaded
Open this post in threaded view
|

Re: help with \newcommand

Michael Sharpe
In reply to this post by Ramón Figueroa-Centeno

On Jun 5, 2010, at 7:10 AM, Ramón Figueroa-Centeno wrote:

>
> Aloha,
>
> I want to define a new command that will make my life a lot easier. Here is
> what I would like:
>
> \newcommand{\foo}[1]{#1 $\#1$}
>
> So that I could invoke it as "\foo{aleph}" I get the same as if I typed
> "aleph $\aleph$" (no backslash on the first character).
>
> Also, much harder (I think):
>
> a command that would do URL encoding right from within LaTeX to pass
> arguments to the hyperref package:
>
> \newcommand{\foo2}[1]{\href{<#1 url encoded>}{#1}}
>
> so that "\foo2{Hello you}" would yield the same as typing
> "\href{http://Hello%20you}{Hello you}"
>
> Thanks,
>


You can encode spaces using
\makeatletter
\def\encspace#1{\encspace@i#1  \@empty}
\def\encspace@i#1 #2{%
#1%
\ifx#2\@empty\else\%20\expandafter\encspace@i\fi
#2}
\makeatother

so that \encspace{This used to have spaces} produces This%20used%20to%20have%20spaces.

Michael----------- Please Consult the Following Before Posting -----------
TeX FAQ: http://www.tex.ac.uk/faq
List Reminders and Etiquette: http://email.esm.psu.edu/mac-tex/
List Archive: http://tug.org/pipermail/macostex-archives/
TeX on Mac OS X Website: http://mactex-wiki.tug.org/
List Info: http://email.esm.psu.edu/mailman/listinfo/macosx-tex

Reply | Threaded
Open this post in threaded view
|

Re: help with \newcommand

Ramón Figueroa-Centeno
Aloha Michael (and all who might read this),

Now, your code is really useful to me. But to really solve my problem I would like it to convert more than spaces If you show me how to do four character pairs say " " to "%20", "\" to "%5C" and "%" to "%25" and "ó" to "\%97", then  I can hard code all the other character pairs that interest me and place the (very long) command in a .sty file, that defines a newcommand named \urlencode, which then can be used to do things like \href{\urlencode{ \%ó}}{ \%ó} instead of \href{\%20\%5C\%25\%97}{ \%ó} (using hyperref).

Moreover if the user had a way to add encodings not hard coded in, say \tr{ñ}{\%96}, then that would be the cats meow.

Thanks,

Ramón
Reply | Threaded
Open this post in threaded view
|

Re: help with \newcommand

Ramón Figueroa-Centeno
Aloha,

Will Robertson provided me with this code (in 20 minutes, unbelievable):

\documentclass{article}
\usepackage{expl3}
\ExplSyntaxOn
\makeatletter
\tex_outer:D \cs_new:Npn \encdef #1 {
  \begingroup
  \@sanitize
  \char_make_space:n {32}
  \afterassignment\endgroup
  \aftergroup \process_test_string:N
  \aftergroup #1
  \tl_gset:Nx #1
}
\cs_generate_variant:Nn \tl_replace_all_in:Nnn {Nxx}
\char_make_other:N \%
\char_make_comment:N \^^A
\cs_new:Npn \process_test_string:N #1 {
  \tl_replace_all_in:Nxx #1 {%} {%25}
  \tl_replace_all_in:Nxx #1 {~} {%20}
  \tl_replace_all_in:Nxx #1 {\@backslashchar} {%5C}
  \tl_replace_all_in:Nxx #1 {ó} {%97}
}
\char_make_comment:N \%
\makeatother
\ExplSyntaxOff
\begin{document}
\encdef\teststring{test string with \slash %percent and accents: ó}
\texttt\teststring
\end{document}

Awesome,

Ramón