5

I want to add my mobile number in a pdf line being generated using LaTeX

how I want it to render: (+91) 7887-61x-xxx

usage: \mobile{91}{788761xxxx}

% Usage: \mobile{<countrycode><mobile number>}
\newcommand*{\mobile}[2]{\def\@countrycode{#1}\def\@mobileno{#2}}
\newcommand*{\countrycode}[1]{\def\@countrycode{#1}}
\newcommand*{\mobileno}[1]{\def\@mobileno{#1}}

The purpose to separate countrycode is bcz I wanto use WhatsApp API using following code:

href{https://wa.me/\@countrycode\@mobileno}{(+\@countrycode) \@mobileno}

How do I add spaces/hyphens in above case using LaTeX?

I want the url to be https://wa.me/91788761xxxx but text to be (+91)7887-61x-xxx

3
  • Welcome to TeX.SE.
    – Mico
    Commented Nov 20, 2021 at 12:34
  • Do you have any need to represent mobile numbers from other areas? For example, fhe pattern is different in North America: (+1)(123) 456-7890 Commented Nov 20, 2021 at 15:10
  • @barbarabeeton, no I just wanted to customize for my local format as I'm using it for resume/cv only Commented Nov 20, 2021 at 15:14

3 Answers 3

7

You can also change the hyphenation format:

\documentclass{article}
\usepackage{hyperref}

\NewDocumentCommand{\mobile}{O{433}mm}
 {
  \href{https://wa.me/#2#3}{\printmobile[#1]{#2}{#3}}
 }

\ExplSyntaxOn
\NewDocumentCommand{\printmobile}{O{433}mm}
 {
  \saste_mobile:nnn { #1 } { #2 } { #3 }
 }

\int_new:N \l__saste_mobile_index_int

\cs_new_protected:Nn \saste_mobile:nnn
 {
  (+#2)\nobreakspace\__saste_mobile:nn { #1 } { #3 }
 }

\cs_new_protected:Nn \__saste_mobile:nn
 {
  \int_set:Nn \l__saste_mobile_index_int { \tl_item:nn { #1 } { 1 } }
  \tl_range:nnn { #2 } { 1 } { \l__saste_mobile_index_int } % first group
  \int_step_inline:nn { \tl_count:n { #1 } - 1 }
   {
    -
    \tl_range:nnn { #2 }
     { \l__saste_mobile_index_int + 1 } % start point
     { \l__saste_mobile_index_int + \tl_item:nn { #1 } { ##1+1 } } % end point
    \int_set:Nn \l__saste_mobile_index_int
     { \l__saste_mobile_index_int + \tl_item:nn { #1 } { ##1+1 } } % for next cycle
   }
 }

\ExplSyntaxOff

\begin{document}

\mobile{91}{1234567890} \mobile[342]{91}{123456789}

\end{document}

The optional argument is a sequence of digits that specifies how many digits from the following number go in each of the hyphen-separated groups. Thus different national conventions can be accommodated. The default is 433.

In the picture the URL corresponding to the first number is shown through hovering on it.

enter image description here

Based on the above code, we can also make much more customizations.

With \setmobile, which can go anywhere including the document preamble, you can set the options to be used from that point on (but it obeys the standard grouping rules).

\documentclass{article}
\usepackage{hyperref}

\NewDocumentCommand{\mobile}{O{}mm}
 {
  \href{https://wa.me/#2#3}{\printmobile[#1]{#2}{#3}}
 }

\ExplSyntaxOn
\NewDocumentCommand{\printmobile}{O{}mm}
 {
  \saste_mobile:nnn { #1 } { #2 } { #3 }
 }

\NewDocumentCommand{\setmobile}{m}
 {
  \keys_set:nn { saste/mobile } { #1 }
 }

\int_new:N \l__saste_mobile_index_int

\keys_define:nn { saste/mobile }
 {
  parens   .bool_set:N         = \l__saste_mobile_parens_bool,
  parens   .default:n          = true,
  parens   .initial:n          = true,
  noparens .bool_set_inverse:N = \l__saste_mobile_parens_bool,
  noparens .default:n          = true,
  presep   .tl_set:N           = \l__saste_mobile_presep_tl,
  presep   .initial:n          = \nobreakspace,
  sep      .tl_set:N           = \l__saste_mobile_sep_tl,
  sep      .initial:n          = -,
  format   .tl_set:N           = \l__saste_mobile_format_tl,
  format   .initial:n          = 433,
 }

\cs_new_protected:Nn \saste_mobile:nnn
 {
  \group_begin:
  \keys_set:nn { saste/mobile } { #1 }
  \bool_if:NT \l__saste_mobile_parens_bool { ( }
  +#2
  \bool_if:NT \l__saste_mobile_parens_bool { ) }
  \l__saste_mobile_presep_tl
  \__saste_mobile:n { #3 }
  \group_end:
 }

\cs_new_protected:Nn \__saste_mobile:n
 {
  \int_set:Nn \l__saste_mobile_index_int { \tl_item:Nn \l__saste_mobile_format_tl { 1 } }
  \tl_range:nnn { #1 } { 1 } { \l__saste_mobile_index_int } % first group
  \int_step_inline:nn { \tl_count:N \l__saste_mobile_format_tl - 1 }
   {
    \l__saste_mobile_sep_tl
    \tl_range:nnn { #1 }
     { \l__saste_mobile_index_int + 1 } % start point
     { \l__saste_mobile_index_int + \tl_item:Nn \l__saste_mobile_format_tl { ##1+1 } } % end point
    \int_set:Nn \l__saste_mobile_index_int
     { \l__saste_mobile_index_int + \tl_item:Nn \l__saste_mobile_format_tl { ##1+1 } } % for next cycle
   }
 }

\ExplSyntaxOff

\begin{document}

\mobile{91}{1234567890}

\bigskip\bigskip

\mobile[format=342]{91}{123456789}

\mobile[noparens,presep=\,,sep=\,,format=333]{91}{123456789}

\setmobile{noparens,presep=\,,sep=\,,format=433}

\mobile{91}{1234567890}

\end{document}

enter image description here

2
  • It's a really great solution, I'm a novice when it comes to writing using LaTeX, the document I'm trying to edit is 'Awesome Resume' - overleaf.com/latex/templates/awesome-cv/dfnvtnhzhhbm can u guide me a little to implement this change while keeping the well organized structured code has? Commented Nov 20, 2021 at 15:19
  • @theSwapnilSaste Just place this code, from the first \NewDocumentCommand up to \ExplSyntaxOff inclusive in the CONFIGURATIONS part.
    – egreg
    Commented Nov 20, 2021 at 21:06
3

If the digits are fixed you may do something like this

\newcommand*\countrycode{\gdef\@countrycode}
\newcommand*\mobileno{\gdef\@mobileno}

\newcommand*\mobile[2]{\countrycode{#1}\expandafter\mobileno\expandafter{\mobileA#2}}
\def\mobileA#1#2#3#4#5#6#7{#1#2#3#4-#5#6#7-}

Then after running \mobile{91}{788761xxxx} you would get 7887-61x-xxx in \@mobileno, but it is not clear if this what you want.

2
  • I want the url to be https://wa.me/91788761xxxx but text to be 7887-61x-xxx Commented Nov 20, 2021 at 12:39
  • then just use \mobileA where you want the hyphens, for example with your original code \expandafter\mobileA \@mobile
    – plante
    Commented Nov 20, 2021 at 12:40
3

If you're in a position to use LuaLaTeX to compile your document, the following solution may be of interest to you. It defines a "wrapper macro" called \mobile, which passes its arguments to a Lua function called mobile (how unoriginal...). The Lua function, in turn, makes use of Lua's string.sub ("subset (of a string)") built-in function to get the job done.

enter image description here

% !TEX TS-program = lualatex
\documentclass{article}
\directlua{
  function mobile ( a , b )
    tex.sprint ( "(+"..a..") "..b:sub(1,4).."-"..b:sub(5,7).."-"..b:sub(8) )
  end
}
% define a LaTeX wrapper macro:
\newcommand\mobile[2]{\directlua{mobile ( "#1" , "#2" )}}

\begin{document}
output of \verb+\mobile{91}{788761xxxx}+:
\mobile{91}{788761xxxx}
\end{document}
1
  • I could add that the LaTeX macro \mobile is expandable (in the TeX-specific sense of the word) and hence can be used in the arguments of other TeX and LaTeX macros,
    – Mico
    Commented Nov 20, 2021 at 12:46

You must log in to answer this question.

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