2
$\begingroup$

I have long notebook with several cells styles (Input, Output, Text, Section, Subsection,...), and I want to find words in the notebook only in the cells have same style.

It is very annoying when you have long notebook with same words in several cell styles and you want to find the words only in certain cell style.

For example, if I have the following notebook :

CreateDocument[{TextCell["test", "Section"], 
   ExpressionCell[test, "Input"], TextCell["test"], 
   TextCell["head one", "Section"], TextCell["test", "Section"], 
   ExpressionCell[Sin[x], "Input"]}];

How can I find the word test only in the Section style cells. If I use Find (Ctrl+f) and click Find Next, Find will search the next match wherever it is. I want the searching of the word to be only within Section style cells.

enter image description here

$\endgroup$

1 Answer 1

4
$\begingroup$
nb = CreateDocument[{TextCell["test", "Section"], 
   ExpressionCell[test, "Input"], TextCell["test"], 
   TextCell["head one", "Section"], TextCell["test", "Section"], 
   ExpressionCell[Sin[x], "Input"]}]

You can use NotebookFind on a subset of cells.

If[SelectionMove[#, Before, Cell]; NotebookFind[#, "test"] =!= $Failed, 
   #, 
   Nothing
] & /@ Cells[nb, CellStyle -> "Section"]

enter image description here

$\endgroup$
7
  • $\begingroup$ That is great. can you tell me how can I access the content of certain cell through its CellObject. For example, if I want to access the content of the Cell associated to the first CellObjec in the Out[47] in your answer, what should I do? The idea is I want to filter the result of the Cells[nb, CellStyle -> "Section"] and get only those CellObject associated to Cells containing "test". $\endgroup$ Feb 1, 2016 at 3:23
  • $\begingroup$ @Algohi It is already done. As you can see only two cells are returned out of three. $\endgroup$
    – Kuba
    Feb 1, 2016 at 6:48
  • $\begingroup$ Sorry, my mistake. Thank you $\endgroup$ Feb 1, 2016 at 17:12
  • $\begingroup$ @Algohi Great ;) tell my if you want to extend this or something. I don't have much time lately but I'm reading what's going on. Good luck. $\endgroup$
    – Kuba
    Feb 1, 2016 at 22:05
  • $\begingroup$ Is there a way to not let the result depend on the current position of the cursor. I am getting some weird results where every NotebookFind gives $Failed depending on the position of the cursor. (I mean, I get the expected behaviour if the cursor is at the top of the notebook. If it is at the end all NotebookFind instances give $Failed.) From "If obj is a NotebookObject, then the find operation starts where the selection is in the notebook." I would have expected that the cursor position should not affect CellObject but it seems that it does. $\endgroup$
    – Kvothe
    May 19, 2022 at 12:11

Your Answer

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

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