0
$\begingroup$

Consider an expression with a list of options at its end, like

Clear[h];  (* Example of something which has options. 
              I want to change one or more of them later *)
hh = H[a, b, c, d -> e, f -> g, h -> j]

Of course, I can get all Options of hh like this:

Options[hh] (* the options associated with symbol hh *)

which returns: {d -> e, f -> g, h -> j}.

Also I can quickly read the particular option of h like this:

Options[hh, h] (* the particular option in my test, which I want to 
change such that it becomes h->i *)

which returns: {h -> j}.

Now I try

Options[hh, h] = {h -> i}  (* this does NOT WORK *)
Options::argx: Options called with 2 arguments; 1 argument is expected. >>
{h -> i}

My next idea is

SetOptions[hh, h -> i]  (* this does NOT WORK *)
SetOptions::sstm: Argument H[a,b,c,d->e,f->g,h->j] is not a symbol or a stream. >>
SetOptions[H[a, b, c, d -> e, f -> g, h -> j], h -> i]

What I am after: A method to programmatically change parts in an expression which are formulate as rules.

1.) A method to strip off all those rules together (in the example above leaving me with H[a, b, c]).

2.) A method to exchange one specific rule (identified by lhs) of the form lhs->rhs by some other expression of rhs.

3.) A method to exchange several such rules at once by new rhs-values.

Of course, in the example above I could have used hh[[6]]=(h->i), but I consider this method unsportsmanlike in this context. I want to make use of Mma finding the right place identified by lhs rather than look at the expression and count the position.

(This all is part of my trials to programmatically change the formatting of cells in an existing notebook, e.g. getting rid of direct formatting attributes of cells and ideally making proper prototypes in the stylesheetfor calls with special formatting needs.)

$\endgroup$
3
$\begingroup$

I think you just need ReplaceAll

  1. A method to strip off all those rules together (in the example above leaving me with H[a, b, c]).

    hh /. { _Rule -> Sequence[]}
    (* H[a, b, c] *)
    
  2. A method to exchange one specific rule (identified by lhs) of the form lhs->rhs by some other expression of rhs.

    hh /. {(f -> _) -> (f -> x)}
    (* H[a, b, c, d -> e, f -> x, h -> j] *)
    
  3. A method to exchange several such rues at once by new rhs-values.

    hh /. {(f -> _) -> (f -> x), (h -> _) -> (h -> y)}
    (* H[a, b, c, d -> e, f -> x, h -> y] *)
    

Hope this helps.

$\endgroup$
  • $\begingroup$ Edmund, thank you. The stumbling blocks for me were to realize the patterns Rule for any rule and also the pattern lhs-> for a rule with any rhs. Thank yiu, this is exactly what I was after. $\endgroup$ – Adalbert Hanßen Apr 2 '16 at 6:39
0
$\begingroup$

I have found one thing for the part of the problem, how to get rid of direct formatting:

There is a function Format>ClearFormatting linked to Shift+Ctl+Space to get rid of all direct formatting attached to the cells. Special formatting within them (like bold or underline od italic) are not affected by it.

This function also works for groups of cells. Unlike my other approach, which works like this and requires the notebook to act upon open and the cells to be treated as marked (but the marking not going beyond groups of cells grouped together, I encountered problems with that) and I need a separate notebook like this one:

meantnb=Notebooks["path to meant notebook.nb"];
nb=meantnb[[1]]; (* a list of NotebookObject is returned, need it wo. {} *)
sel=SelectedCells[nb];
zelle=NotebookRead[sel];
anz=Length[zelle];       (* number of selected cells *)
Print["anz=",anz];
For[i=1
   ,i<=anz
   ,i++
   ,len=Length[zelle[[i]]];
    Print["  len=",len];
    zelle[[i]]=Take[zelle[[i]],2]; (* get rid of options *)
   ];
 NotebookWrite[nb,zelle]

This code snipplet might serve as a start point to add options. Unfortunately it has side effects on cell grouping in the notebook written-to: They are ungrouped if the last cell of a group is part of the selection. Is someone out there who could come up with something just like that?

$\endgroup$

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.