3
$\begingroup$

I have the following palette:

CreatePalette[Column[
   (Button[Style[#1, 20, FontFamily -> 
              "Calibri"], FrontEndTokenExecute[
            InputNotebook[], "Style", #1], 
          Appearance -> "Palette", 
          ImageSize -> 250] & ) /@ 
     {"Title", "Subtitle", "Subsubtitle", 
       "Section", "Subsection", 
       "Subsubsection", "Text", 
       "DisplayFormula", 
       "DisplayFormulaNumbered"}, 
   Spacings -> 0]]; 

I want to add two extra buttons to the bottom of the palette:

  1. A button that copies the number of a formula in the selected DisplayFormulaNumbered cell;
  2. A button that pastes this as a reference wherever I choose (ideally in brackets).

I do have a palette which copies cell references:

CreatePalette[Button["Copy cell ref", 
 Module[{cell, tags, tag, createTag}, 
   cell = First[Cells[NotebookSelection[
            InputNotebook[]], CellStyle -> 
            "DisplayFormulaNumbered"], 
        MessageDialog[
           "Select an equation cell!"]; 
         Return[]]; tags = Replace[
        CurrentValue[cell, CellTags], 
        t : Except[_List] :> {t}]; 
    createTag := With[
        {newTag = CreateUUID["eqnID-"]}, 
        SetOptions[cell, CellTags -> 
             Append[Select[tags, Not @* 
                   StringMatchQ["eqnID-*"]], 
               newTag]]; newTag]; 
    tag = SelectFirst[tags, StringMatchQ[
          "eqnID-*"], createTag]; 
    If[Length[Cells[InputNotebook[], 
            CellTags -> tag]] > 1, 
      MessageDialog["Multiple cells with same \
 tag found!\n     Tag of current cell will be \
 regenerated."]; tag = createTag]; 
    With[{tag = tag}, CopyToClipboard[
        Button[Row[{"(", Dynamic[CurrentValue[
                  First[Cells[CellTags -> tag], 
                    $Failed], {"CounterValue", 
                    "DisplayFormulaNumbered"}]], 
              ")"}], Appearance -> None]]]]], 
 Saveable -> False, WindowTitle -> 
 "Equation linking"]

...but I can't figure out how to add this functionality to the first palette, and add the Paste function.

$\endgroup$
0

1 Answer 1

1
$\begingroup$
stylebuttons = (Button[Style[#1, 20, FontFamily -> "Calibri"], 
      FrontEndTokenExecute[InputNotebook[], "Style", #1], 
      Appearance -> "Palette", ImageSize -> 250] &) /@ {"Title", 
    "Subtitle", "Subsubtitle", "Section", "Subsection", 
    "Subsubsection", "Text", "DisplayFormula", 
    "DisplayFormulaNumbered"};

button2 = Button[Style["Copy cell ref", 20, FontFamily -> "Calibri"], 
   Module[{cell, tags, tag, createTag}, 
    cell = First[Cells[NotebookSelection[InputNotebook[]], 
       CellStyle -> "DisplayFormulaNumbered"], 
      MessageDialog["Select an equation cell!"];
      Return[]]; 
    tags = Replace[CurrentValue[cell, CellTags], 
      t : Except[_List] :> {t}];
    createTag := With[{newTag = CreateUUID["eqnID-"]}, 
      SetOptions[cell, 
       CellTags -> Append[Select[tags, Not@*StringMatchQ["eqnID-*"]], newTag]]; 
      newTag];
    tag = SelectFirst[tags, StringMatchQ["eqnID-*"], createTag];
    If[Length[Cells[InputNotebook[], CellTags -> tag]] > 1, 
     MessageDialog["Multiple cells with same  tag found!\n     Tag of current cell \
will be  regenerated."]; tag = createTag];
    With[{tag = tag}, 
     CopyToClipboard[Button[Row[{"(", 
         Dynamic[CurrentValue[First[Cells[CellTags -> tag], $Failed], {"CounterValue", 
            "DisplayFormulaNumbered"}]], ")"}], 
       Appearance -> None]]]], Appearance -> None];

 pastebutton = Button[Style["Paste", 20, FontFamily -> "Calibri"], Paste[], 
      Appearance -> None];

Combine the buttons using Join:

CreatePalette[Column[Join[stylebuttons, {button2, pastebutton}], 
  Spacings -> 0, Alignment -> Center], 
 Saveable -> False,  WindowTitle -> "Cell Styles & Cell Ref"]

enter image description here

$\endgroup$
3
  • $\begingroup$ Thanks @kglr.Is it possible to make the last two buttons look the same as the rest? $\endgroup$ Jan 5, 2021 at 14:20
  • $\begingroup$ @RichardBurke-Ward, please see the updated version. $\endgroup$
    – kglr
    Jan 5, 2021 at 14:28
  • $\begingroup$ Fantastic. Many thanks. $\endgroup$ Jan 5, 2021 at 14:43

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.