1

I have XML code which I need to copy paste. Here's is the code

<joint name="joint1" type="continuous">
  <parent link="link1"/>
  <child link="link2"/>
  <origin xyz="5 3 0" rpy="0 0 0" />
</joint>

I have used accsup to make it copy/paste-able as follows:

\usepackage[T1]{fontenc}
\usepackage{textcomp}
\usepackage{listings}
\lstset{ 
upquote=true,
columns=fullflexible,
literate={\ }{{\copyablespace}}1
}

\usepackage[space=true]{accsupp}
% requires the latest version of package accsupp
\newcommand{\copyablespace}{
\BeginAccSupp{method=hex,ActualText=00A0}
\ %
\EndAccSupp{}
}

I am new to LaTex and am not sure what exactly is happening here. But the BeginAccSupp part is replacing each space by 3 spaces in the pdf and as well when I copy paste.

<joint   name="joint1"   type="continuous">
    <parent   link="link1"/>
    <child   link="link2"/>
    <origin   xyz="5   3   0"   rpy="0   0   0"   />
</joint>

What am I doing wrong?

Edit !: After @Phelype Oleinik's solution, the PDF looks fine, also the indentation after I copy/paste is good, but the spaces in between the line are still the same

<joint   name="joint1"   type="continuous">
 <parent   link="link1"/>
 <child   link="link2"/>
 <origin   xyz="5   3   0"   rpy="0   0   0"   />
</joint>

Edit 2: A hack that works (not for the string spaces though) is replacing the spaces with tabs in the {lstlisting}.

So the code in tex looks like:

<joint  name="joint1" type="continuous">
  <parent   link="link1"/>
  <child    link="link2"/>
  <origin   xyz="5 3 0" rpy="0 0 0" />
</joint>

And the copy/pasted code looks liḱe:

<joint name="joint1"   type="continuous">
 <parent link="link1"/>
 <child link="link2"/>
 <origin xyz="5   3   0"   rpy="0   0   0"   />
</joint

But not sure if I want to go with this hack.

harsh
  • 111
  • 2
  • You have spurious spaces in your \copyablespace macro. If you put a % right after each line of the macro the problem should vanish. Use this: \newcommand{\copyablespace}{% \BeginAccSupp{method=hex,ActualText=00A0}% \ % \EndAccSupp{}% } – Phelype Oleinik Feb 15 '18 at 14:18
  • Thanks! Now the PDF looks fine but the copy pasted code is still the same. BTW what exactly does % do? – harsh Feb 15 '18 at 14:24
  • 1
    Welcome to TeX.SX! Missing end-of-line protection is a common error. – egreg Feb 15 '18 at 14:26
  • Be careful to add % correctly: \newcommand{\copyablespace}{%, \BeginAccSupp{method=hex,ActualText=00A0}% and \EndAccSupp{}% – egreg Feb 15 '18 at 14:51
  • is it something to do with unicode in \BeginAccSupp? I could not paste anything after the 1st space without unicode, but with it, each space is replaced by 3. – harsh Feb 15 '18 at 15:01
  • FWIW, I get the correct spaces if I remove the literate option. – egreg Feb 15 '18 at 15:05
  • If I remove literate option, there's not indentation after pasting the code. – harsh Feb 15 '18 at 15:07
  • 1
    @harsh When TeX read a single new line, it interprets it as a space (that's why you can write a paragraph in multiple lines of code). This happens inside your macro. The % char is added to the end of the line to make TeX ignore what comes after, including the new line. For a more lengthy explanation see the first part of my answer here. – Phelype Oleinik Feb 15 '18 at 15:12
  • Why don't you embedd an xml-file with your code to the PDF and refer to it in the text? There are some packages, like attachfile, attachfile2, embedfile. See here: https://tex.stackexchange.com/q/19025/4736 how to do that. – Keks Dose Feb 15 '18 at 15:16
  • 4
    I’m voting to close this question because it was (maybe partially) solved in the comments – Alessandro Cuttin May 21 '20 at 07:58

0 Answers0