0
$\begingroup$

I use the following code output and need to retrieve the position of the part containing the blue-coloured integer 4 and the part itself (Edited to indicate both are needed). The displayed code looks a bit daunting as it is given in FullForm. If you copy the code in a notebook, the output of each part will be brief and clear. I had to use FullForm due to indicate the colour selections involved.

NOTE 1: my main query regards why the proposed method does not work. I appreciate alternative solution methods, but want to learn from mistakes. I don't see why the proposed method fails. My approach seems correct, yet gives the wrong answer. Any tips on what goes wrong would be very welcome.

Note 2: See the last two lines of this post querying why selecting on IntegerQ yields a wrong result.

Part 1:

List[Placed[List[
      Style[3, Rule[LineColor, RGBColor[1, 0, 0]], 
       Rule[FrontFaceColor, RGBColor[1, 0, 0]], 
       Rule[BackFaceColor, RGBColor[1, 0, 0]], 
       Rule[GraphicsColor, RGBColor[1, 0, 0]], 
       Rule[FontColor, RGBColor[1, 0, 0]]], 
      Style[4, Rule[LineColor, RGBColor[0, 0, 1]], 
       Rule[FrontFaceColor, RGBColor[0, 0, 1]], 
       Rule[BackFaceColor, RGBColor[0, 0, 1]], 
       Rule[GraphicsColor, RGBColor[0, 0, 1]], 
       Rule[FontColor, RGBColor[0, 0, 1]]]], List[Before, After]], Placed[
     List[Style[4, Rule[LineColor, RGBColor[1, 0, 0]], 
       Rule[FrontFaceColor, RGBColor[1, 0, 0]], 
       Rule[BackFaceColor, RGBColor[1, 0, 0]], 
       Rule[GraphicsColor, RGBColor[1, 0, 0]], 
       Rule[FontColor, RGBColor[1, 0, 0]]], 
      Style[3, Rule[LineColor, RGBColor[0, 0, 1]], 
       Rule[FrontFaceColor, RGBColor[0, 0, 1]], 
       Rule[BackFaceColor, RGBColor[0, 0, 1]], 
       Rule[GraphicsColor, RGBColor[0, 0, 1]], 
       Rule[FontColor, RGBColor[0, 0, 1]]]], List[Before, After]]]

To retrieve the expression containing the blue 4 I tried:

Part 2:

        Select[List[Placed[List[
              Style[3, Rule[LineColor, RGBColor[1, 0, 0]], 
               Rule[FrontFaceColor, RGBColor[1, 0, 0]], 
               Rule[BackFaceColor, RGBColor[1, 0, 0]], 
               Rule[GraphicsColor, RGBColor[1, 0, 0]], 
               Rule[FontColor, RGBColor[1, 0, 0]]], 
              Style[4, Rule[LineColor, RGBColor[0, 0, 1]], 
               Rule[FrontFaceColor, RGBColor[0, 0, 1]], 
               Rule[BackFaceColor, RGBColor[0, 0, 1]], 
               Rule[GraphicsColor, RGBColor[0, 0, 1]], 
               Rule[FontColor, RGBColor[0, 0, 1]]]], List[Before, After]], Placed[
             List[Style[4, Rule[LineColor, RGBColor[1, 0, 0]], 
               Rule[FrontFaceColor, RGBColor[1, 0, 0]], 
               Rule[BackFaceColor, RGBColor[1, 0, 0]], 
               Rule[GraphicsColor, RGBColor[1, 0, 0]], 
               Rule[FontColor, RGBColor[1, 0, 0]]], 
              Style[3, Rule[LineColor, RGBColor[0, 0, 1]], 
               Rule[FrontFaceColor, RGBColor[0, 0, 1]], 
               Rule[BackFaceColor, RGBColor[0, 0, 1]], 
               Rule[GraphicsColor, RGBColor[0, 0, 1]], 
               Rule[FontColor, RGBColor[0, 0, 1]]]], List[Before, After]]], Select[#[[1]][[2]], IntegerQ] & 
== 4]

Which produces { }

However

Part 3

 a = Placed[
    List[Style[3, Rule[LineColor, RGBColor[1, 0, 0]], 
      Rule[FrontFaceColor, RGBColor[1, 0, 0]], 
      Rule[BackFaceColor, RGBColor[1, 0, 0]], 
      Rule[GraphicsColor, RGBColor[1, 0, 0]], 
      Rule[FontColor, RGBColor[1, 0, 0]]], 
     Style[4, Rule[LineColor, RGBColor[0, 0, 1]], 
      Rule[FrontFaceColor, RGBColor[0, 0, 1]], 
      Rule[BackFaceColor, RGBColor[0, 0, 1]], 
      Rule[GraphicsColor, RGBColor[0, 0, 1]], 
      Rule[FontColor, RGBColor[0, 0, 1]]]], 
    List[Before, After]][[1]][[2]]

Select[a,IntegerQ]

produces 4 as expected.

Hence I would have expected Part 2 to produce the expression containing the blue label 4, i.e. the following result

Placed[List[
          Style[3, Rule[LineColor, RGBColor[1, 0, 0]], 
           Rule[FrontFaceColor, RGBColor[1, 0, 0]], 
           Rule[BackFaceColor, RGBColor[1, 0, 0]], 
           Rule[GraphicsColor, RGBColor[1, 0, 0]], 
           Rule[FontColor, RGBColor[1, 0, 0]]], 
          Style[4, Rule[LineColor, RGBColor[0, 0, 1]], 
           Rule[FrontFaceColor, RGBColor[0, 0, 1]], 
           Rule[BackFaceColor, RGBColor[0, 0, 1]], 
           Rule[GraphicsColor, RGBColor[0, 0, 1]], 
           Rule[FontColor, RGBColor[0, 0, 1]]]], List[Before, After]]

On closer inspection I tried the following:

FullForm[Select[a, IntegerQ]]

which results in

Style[4]

However, Style[4] should not be the result of a selection for integers.

What is going on?

Note that

IntegerQ[Style[4]] 

yields False.

Yet Style[4] is what is being produced by FullForm[Select[a, IntegerQ]].

Can you clarify why this is the case?

Is this a bug? Or am I missing something?

$\endgroup$
6
  • 1
    $\begingroup$ I am confused by your problem. Do you want the position of the blue 4, or the expression that generates the blue 4, or something else? $\endgroup$
    – MarcoB
    Commented Mar 24, 2021 at 16:35
  • $\begingroup$ Actually, both. I will amend in an edit $\endgroup$ Commented Mar 24, 2021 at 18:22
  • $\begingroup$ Edited it accordingly $\endgroup$ Commented Mar 24, 2021 at 18:39
  • $\begingroup$ LineColor, FrontFaceColor, BackFaceColor and GraphicsColor are not options for Style. Your code could be written {Placed[{Style[3, FontColor -> RGBColor[1, 0, 0]], Style[4, FontColor -> RGBColor[0, 0, 1]]}, {Before, After}], Placed[{Style[4, FontColor -> RGBColor[1, 0, 0]], Style[3, FontColor -> RGBColor[0, 0, 1]]}, {Before, After}]} $\endgroup$
    – Ben Izd
    Commented Mar 24, 2021 at 19:10
  • $\begingroup$ @BenyIzd I did not choose this format. It is Mathematica output, i.e generated by Mathematica code beyond my control. Not sure if you are saying that the code should not be written this way or that the wrong output I indicate in the post is due to my handling LineColor etc in a wrong way? $\endgroup$ Commented Mar 24, 2021 at 19:20

3 Answers 3

1
$\begingroup$

Why does the above example result in: Style[4]?

Consider the example:

Select[{c0,c1},#==c0&]

(* { c0 } *)

No problem here. Now we replace "{..}" by "f[..]":

Select[f[c0, c1], # == c0 &]

(* f[c0] *)

Therefore, the head of the expression is retained. Now, consider:

a // FullForm

enter image description here

a has a head of Style and a first element of 4. Therefore:

Select[a, IntegerQ] // FullForm

will pick out 4 and wrap it with the header Style: Style[4]

$\endgroup$
1
  • $\begingroup$ Thanks Daniel, that is very clear. I misinterpreted select to return the selected element for lists. I understand the motivation to retain the head to deal with the case of returning several elements satisfying the condition. $\endgroup$ Commented Mar 25, 2021 at 9:41
2
$\begingroup$
lst = List[
  Placed[List[
    Style[3, Rule[LineColor, RGBColor[1, 0, 0]], 
     Rule[FrontFaceColor, RGBColor[1, 0, 0]], 
     Rule[BackFaceColor, RGBColor[1, 0, 0]], 
     Rule[GraphicsColor, RGBColor[1, 0, 0]], 
     Rule[FontColor, RGBColor[1, 0, 0]]], 
    Style[4, Rule[LineColor, RGBColor[0, 0, 1]], 
     Rule[FrontFaceColor, RGBColor[0, 0, 1]], 
     Rule[BackFaceColor, RGBColor[0, 0, 1]], 
     Rule[GraphicsColor, RGBColor[0, 0, 1]], 
     Rule[FontColor, RGBColor[0, 0, 1]]]], List[Before, After]], 
  Placed[List[
    Style[4, Rule[LineColor, RGBColor[1, 0, 0]], 
     Rule[FrontFaceColor, RGBColor[1, 0, 0]], 
     Rule[BackFaceColor, RGBColor[1, 0, 0]], 
     Rule[GraphicsColor, RGBColor[1, 0, 0]], 
     Rule[FontColor, RGBColor[1, 0, 0]]], 
    Style[3, Rule[LineColor, RGBColor[0, 0, 1]], 
     Rule[FrontFaceColor, RGBColor[0, 0, 1]], 
     Rule[BackFaceColor, RGBColor[0, 0, 1]], 
     Rule[GraphicsColor, RGBColor[0, 0, 1]], 
     Rule[FontColor, RGBColor[0, 0, 1]]]], List[Before, After]]]

Finding the element with the blue 4:

Select[lst, 
 MemberQ[#[[1]], 
   Style[4, Rule[LineColor, RGBColor[0, 0, 1]], 
    Rule[FrontFaceColor, RGBColor[0, 0, 1]], 
    Rule[BackFaceColor, RGBColor[0, 0, 1]], 
    Rule[GraphicsColor, RGBColor[0, 0, 1]], 
    Rule[FontColor, RGBColor[0, 0, 1]]]] &]

enter image description here

The position of the element that contains the blue 4:

Position[lst, 
 Style[4, Rule[LineColor, RGBColor[0, 0, 1]], 
  Rule[FrontFaceColor, RGBColor[0, 0, 1]], 
  Rule[BackFaceColor, RGBColor[0, 0, 1]], 
  Rule[GraphicsColor, RGBColor[0, 0, 1]], 
  Rule[FontColor, RGBColor[0, 0, 1]]]]

(* {{1, 1, 2}} *)

lst[[1,1,2]]

enter image description here

$\endgroup$
1
$\begingroup$

I assume that you are asking how to determine the position of the blue 4.

Your problem comes from the fact that pattern are also evaluated and that the output for presentation does not always show the underlying code.

Your list reads:

li=li = List[
  Placed[List[
    Style[3, Rule[LineColor, RGBColor[1, 0, 0]], 
     Rule[FrontFaceColor, RGBColor[1, 0, 0]], 
     Rule[BackFaceColor, RGBColor[1, 0, 0]], 
     Rule[GraphicsColor, RGBColor[1, 0, 0]], 
     Rule[FontColor, RGBColor[1, 0, 0]]], 
    Style[4, Rule[LineColor, RGBColor[0, 0, 1]], 
     Rule[FrontFaceColor, RGBColor[0, 0, 1]], 
     Rule[BackFaceColor, RGBColor[0, 0, 1]], 
     Rule[GraphicsColor, RGBColor[0, 0, 1]], 
     Rule[FontColor, RGBColor[0, 0, 1]]]], List[Before, After]], 
  Placed[List[
    Style[4, Rule[LineColor, RGBColor[1, 0, 0]], 
     Rule[FrontFaceColor, RGBColor[1, 0, 0]], 
     Rule[BackFaceColor, RGBColor[1, 0, 0]], 
     Rule[GraphicsColor, RGBColor[1, 0, 0]], 
     Rule[FontColor, RGBColor[1, 0, 0]]], 
    Style[3, Rule[LineColor, RGBColor[0, 0, 1]], 
     Rule[FrontFaceColor, RGBColor[0, 0, 1]], 
     Rule[BackFaceColor, RGBColor[0, 0, 1]], 
     Rule[GraphicsColor, RGBColor[0, 0, 1]], 
     Rule[FontColor, RGBColor[0, 0, 1]]]], List[Before, After]]]

And we want to determine the positions of terms with a pattern:

pat = Style[n_, Rule[LineColor, RGBColor[0, 0, 1]], __];
Position[li, pat, Infinity] 

(* {{1, 1, 2}} *)

However, pay attention. If we now look at:

li[[1, 1, 2]]
(* 4 *)

this looks unsuspicious like the integer 4. But look at:

li[[1, 1, 2]] //FullForm

we get:

enter image description here

not exactly what we need for further calculations. Therefore to get the integer 4 we need an additional level:

li[[1, 1, 2, 1]]
(* 4 *)

Therefore, the real position is:

{1, 1, 2, 1}
$\endgroup$
1
  • $\begingroup$ Thanks, I get this part, i.e. the fact that a blue 4 looks like a 4 but is a different expression. I don't see why IntegerQ (in my approach) would yield the seemingly wrong result Style[4]. $\endgroup$ Commented Mar 24, 2021 at 20:25

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.