Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I want to write IBANs and noticed that if I write DE12\,1234\,5678\,9123\,4567\,89 I am getting the half space I would expect but when copy-pasting it, the white space vanishes (as mentioned in comment, this seems to be viewer-dependent. I am using Sumatra). This is really nice. But what could I do, if I want to get a full whitespace in the print out, but no whitespace when copying it? In CSS you can separate visually (yielding DE12 1234 5678 9123 4567 89 in the print out) but leave the numbers together (yielding DE12123456789123456789 when copy-pasting it).

First of all my MWE:

% arara: pdflatex

\documentclass{article}

\begin{document}
DE12\,1234\,5678\,9123\,4567\,89

DE12 1234 5678 9123 4567 89
\end{document}

I tried other whitespaces such as \ or ~ or even \,\, but they will always result in dismembered copy-pasting-results.

Is there such a whitespace around, which would do what I want? Maybe (as an extra), it would be cool to have a macro \IBAN{DE12123456789123456789} which will do the separating automatically (the rule is easy: groups of four from left to right).

share|improve this question
1  
You can try the accsupp package to copy a different text. But all that depends very heavily on the viewer, because most of the time there doesn't exist any whitespace-character in the pdf, and the viewer inserts them where it thinks it's right. –  Juri Robl Aug 29 at 13:52
1  
Using Adobe viewer, the half spaces copy/paste as a space. –  Steven B. Segletes Aug 29 at 14:09
    
@StevenB.Segletes interesting. I am using SumatraPDF and there it copies without the half spaces. Thanks Juri, I will have a look at this. But after your two comments, I get the feeling its more a viewer issue than a solvable topic for LaTeX. –  LaRiFaRi Aug 29 at 14:21
    
Would you like to generate a font that encodes space as, for example, U+FFEF? Since it is not a valid unicode character, it should not become space after copy/pasting. –  Symbol 1 Aug 29 at 14:35
    
I am not sure, if I get you right. Why is U+FFEF not a valid unicode character? If you may, I would be glad to see an answer about your idea. The full whitespace in the pdf may be breakable (don't care about that right now) but should vanish when getting copied. –  LaRiFaRi Aug 29 at 14:41

2 Answers 2

up vote 12 down vote accepted

TOTAL REVISION from original misunderstanding.

EDIT again to provide two approaches: (1) \IBAN{} for turning an unspaced string of letters into a spaced IBAN number that nonetheless copy/pastes without the spaces, and (2) \ncs{} for taking a space filled string and making it so that a copy/paste removes the spaces.

THE \IBAN{} MACRO APPROACH

Following up on Juri's suggestion, this works. I have edited to automate the process with the syntax, for example, \IBAN{DE12123456789123456789}. The MWE below checks to make sure unusual IBANs, with only a small number of digits, don't break the algorithm.

\documentclass{article}
\usepackage{accsupp}
\def\IBAN#1{%
  \unskip\def\viewed{}\IBANhelper#1\relax\relax\relax\relax\relax%
  \BeginAccSupp{method=escape,ActualText=#1}%
    \viewed%
  \EndAccSupp{}%
}
\def\IBANhelper#1#2#3#4#5\relax{\edef\viewed{\viewed\ #1#2#3#4}%
  \if\relax#5\relax\else\IBANhelper#5\relax\relax\relax\relax\fi}
\begin{document}
The IBAN number is \IBAN{DE12123456789123456789} if I am not mistaken.

The IBAN number is \IBAN{DE121} if I am not mistaken.

The IBAN number is \IBAN{DE12} if I am not mistaken.

The IBAN number is \IBAN{DE1} if I am not mistaken.

The IBAN number is \IBAN{DE} if I am not mistaken.

The IBAN number is \IBAN{D} if I am not mistaken.

The IBAN number is \IBAN{} if I am not mistaken.
\end{document}

Here is the PDF output:

enter image description here

while the copy/paste output (in Adobe Acrobat) is

The IBAN number is DE12123456789123456789 if I am not mistaken.
The IBAN number is DE121 if I am not mistaken.
The IBAN number is DE12 if I am not mistaken.
The IBAN number is DE1 if I am not mistaken.
The IBAN number is DE if I am not mistaken.
The IBAN number is D if I am not mistaken.
The IBAN number is if I am not mistaken.

I credit this question, Is it possible to provide alternative text to use when copying text from the PDF?, for lernin' me accsupp.


THE \ncs{} (NO-COPY-SPACE) MACRO:

This approach may have more general utility, since it will work for things other than IBAN numbers. In this case, a space-filled string is typeset as is; however, the copy/paste text in the PDF removes all spaces.

\documentclass{article}
\usepackage{accsupp}
\newcommand\ncs[1]{%
  \def\actual{}\ncshelper#1 \relax%
  \BeginAccSupp{method=escape,ActualText=\actual}%
    #1%
  \EndAccSupp{}%
}
\def\ncshelper#1 #2\relax{\edef\actual{\actual#1}%
  \if\relax#2\relax\else\ncshelper#2\relax\fi}
\begin{document}
I like the palindrome \ncs{a man a plan a canal panama}.
\end{document}

It displays as expected,

enter image description here

but copies/pastes as

I like the palindrome amanaplanacanalpanama .
share|improve this answer
    
@StevenB.Segletes Thanks a lot for your time and effort. Works perfectly with Evince and I believe you on Adobe. Sumatra has to be tested later... –  LaRiFaRi Aug 29 at 17:15

I prepared an expl3 version of Steven's answer featuring both \IBAN and \ncs.

\documentclass{article}
\usepackage{xparse,accsupp}
\ExplSyntaxOn

\cs_new_protected:Npn \larifari_iban:n #1
  {
    \int_zero:N \l_tmpa_int
    \tl_map_inline:nn { #1 }
      {
        ##1
        \int_incr:N \l_tmpa_int
        \bool_if:nT
          {
            \int_compare_p:n { \int_mod:nn { \l_tmpa_int } { 4 } = 0 }
            &&
            \int_compare_p:n { \l_tmpa_int < \tl_count:n { #1 } }
          }
          { ~ }
      }
  }

\NewDocumentCommand \IBAN { m }
  {
    \BeginAccSupp{method=escape,ActualText=#1}
      \larifari_iban:n { #1 }
    \EndAccSupp{}
  }

\cs_new_protected:Npn \larifari_ncs:Nn #1#2
  {
    \tl_set:Nn #1 { #2 }
    \tl_remove_all:Nn #1 { ~ }
  }

\tl_new:N \l_larifari_ncs_tl

\NewDocumentCommand \ncs { m }
  {
    \larifari_ncs:Nn \l_larifari_ncs_tl { #1 }
    \BeginAccSupp{method=escape,ActualText=\tl_use:N \l_larifari_ncs_tl}
      #1
    \EndAccSupp{}
  }

\ExplSyntaxOff
\begin{document}
The IBAN number is \IBAN{DE12123456789123456789} if I am not mistaken.

The IBAN number is \IBAN{DE121} if I am not mistaken.

The IBAN number is \IBAN{DE12} if I am not mistaken.

The IBAN number is \IBAN{DE1} if I am not mistaken.

The IBAN number is \IBAN{DE} if I am not mistaken.

The IBAN number is \IBAN{D} if I am not mistaken.

The IBAN number is \IBAN{} if I am not mistaken.

I like the palindrome \ncs{a man a plan a canal panama}.
\end{document}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.