3
$\begingroup$

I am struggling to find a way to use PasteButton to paste the name of an expression (in particular a list) without showing the contents of the expression or without wrapping it in quotes. For example, in this example the values of myList {1,2,3} is pasted rather than the blackened expression name myList.

myList={1,2,3}

PasteButton["Paste myList", myList] 

If instead I use ToString then myList is pasted with quotation marks around it as "myList":

PasteButton["Paste myList", ToString@myList] 

I simply want the expression name myList without quotes around it so I can choose to evaluate it with Shift+Enter or not. There must be something obvious I am missing. Any suggestions?

$\endgroup$
5
  • 1
    $\begingroup$ PasteButton["Paste myList", Defer@myList] $\endgroup$
    – MarcoB
    Nov 15 '15 at 20:57
  • $\begingroup$ @MarcoB Thanks. Any idea how can I do this with PasteButton if I located myList by using Names["Global*"]? For example if Names["Global*"][[1]] is "myList" how can I use PasteButton to paste it as the blackened text myList and not the values of myList or the string "myList"? $\endgroup$
    – user13999
    Nov 15 '15 at 21:03
  • $\begingroup$ That seems convoluted. Perhaps you could edit your question to provide more context explaining what you ultimately want to achieve with these manipulations. $\endgroup$
    – MarcoB
    Nov 15 '15 at 21:08
  • $\begingroup$ @user13999 Hi, I've seen you have accepted only answer for all your questions. Aren't any of others good enough? Please, take a Tour. $\endgroup$
    – Kuba
    Nov 17 '15 at 8:27
  • $\begingroup$ @Kuba Ok, I accepted your answer on the pop-up workspace. Thanks for your help. $\endgroup$
    – user13999
    Nov 18 '15 at 14:58
4
$\begingroup$

You could use ToExpression to convert the string to a symbol, with Defer as the third argument:

symbolname = First@Names["Global`my*"]
(* "myList" *)

PasteButton[ToExpression[symbolname, InputForm, Defer]]
$\endgroup$
1
  • $\begingroup$ Great, thanks for picking up on my added comment. $\endgroup$
    – user13999
    Nov 15 '15 at 23:00
3
$\begingroup$

Or you can use RawBoxes:

PasteButton[ RawBoxes @ First @ Names["Global`*"] ]
$\endgroup$
1
  • $\begingroup$ Cool, never heard of RawBoxes. Thanks. $\endgroup$
    – user13999
    Nov 15 '15 at 23:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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