I'd like to use ClickToCopy to display a matrix equation, and have its TeX form be copyable. I started with something like

ClickToCopy[M == IdentityMatrix[2], 
 ToString@TeXForm[M == IdentityMatrix[2]]]

but then the copied text is a String and not something I can just paste into a tex file:

"M=\\left(\n\\begin{array}{cc}\n 1 & 0 \\\\\n 0 & 1 \\\\\n\

So, following advice here, I added RawBoxes:

ClickToCopy[M == IdentityMatrix[2], 
 RawBoxes@ToString@TeXForm[M == IdentityMatrix[2]]]

This is much closer to being correct, but now I get

M = \left (
   \begin {array} {cc}
         1 & 0 \\
       0 & 1 \\
     \end {array}
    \r ight)

In the last line, "\right" gets an unwanted space. Looking at other examples, this behavior seems to be connected with the backslash, but I'm not sure exactly what makes it happen.

Is there (a) a way of fixing the original ClickToCopy without RawBoxes, or (b) a way to fix this behavior in RawBoxes?

  • $\begingroup$ There is no space. How and where have you pasted it? $\endgroup$ Dec 24, 2023 at 8:56

1 Answer 1


There seems to be a problem when trying to paste it outside of Mathematica when there is indeed a space after \r.

A workaround using Button and CopyToClipboard:

Button[M == IdentityMatrix[2], 
 CopyToClipboard@TeXForm[M == IdentityMatrix[2]], 
 Appearance -> "Frameless"


A better version of the above code that makes the output visually same as if ClickToCopy was used, including the mouse-over label Copy to clipboard.

  Style[M == IdentityMatrix[2], {"Output", "StandardForm"}], Null], 
 CopyToClipboard@TeXForm[M == IdentityMatrix[2]], 
 Appearance -> "Frameless"]

enter image description here

enter image description here

After clicking the output and pasting:

enter image description here

  • $\begingroup$ Thanks! I was hoping to avoid missing out on the nicer look-and-feel of ClickToCopy, but as far as I can tell there's no way to get ClickToCopy to work the way I need, so this is the best move. It would be great if ClickToCopy were updated to include an "External" option, where e.g. strings are copied to the clipboard raw instead of as Wolfram input. $\endgroup$
    – srossd
    Dec 26, 2023 at 18:35
  • 1
    $\begingroup$ @srossd: Yes ClickToCopy is primarily made for using to paste the copied code into Mathematica. I updated my answer with a better code that resembles the appearance and functionality of ClickToCopy. $\endgroup$ Dec 26, 2023 at 19:57
  • $\begingroup$ Oh, that is clever, thank you! $\endgroup$
    – srossd
    Dec 27, 2023 at 16:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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