3
$\begingroup$

I would like to label the atoms of a molecule imported from an XYZ file. The only example that I found is the following (How to label with numbers atoms when MoleculePlot3D):

 mol = Molecule["Formaldehyde"];

 Show[ MoleculePlot3D[mol], 
  Graphics3D[MapThread[Text, 
    {Style[#, 16] & /@ Range[4], mol["AtomCoordinates"]["Magnitudes"]}]]] 

Could you please help me how to modify this code to take the molecule from an XYZ file, say from "ExampleData/caffeine.xyz"? And is there any way to label the atoms individually?

$\endgroup$
6
  • 1
    $\begingroup$ The most direct way would be to replace Molecule["Formaldehyde"] with Import["ExampleData/caffeine.xyz"], and Range[4] with Range[AtomCount[mol]] $\endgroup$
    – Jason B.
    Nov 5, 2021 at 14:40
  • $\begingroup$ @JasonB. Thank you very much! It helped a lot! And is there any way to set individual labels for the atoms? $\endgroup$
    – TobiR
    Nov 5, 2021 at 14:43
  • $\begingroup$ what version of Mathematica are you using? $\endgroup$
    – Jason B.
    Nov 5, 2021 at 14:45
  • $\begingroup$ @JasonB. The version I am using is 12.3. $\endgroup$
    – TobiR
    Nov 5, 2021 at 14:46
  • 2
    $\begingroup$ Replacing "Style[#, 16] & /@ Range[4]" by "Style[#, 16] & /@ {"C", "O", "H", "H"} " should do what you want. $\endgroup$ Nov 5, 2021 at 15:01

1 Answer 1

5
$\begingroup$

I didn't have this functionality as bullet proof as it should be by the time version 12.3 was released, so this option is undocumented. But it does work pretty well, and follows most of the conventions of VertexLabels.

To label all atoms by their index use

mol = Import["ExampleData/caffeine.xyz"];
MoleculePlot3D[mol, AtomLabels -> "AtomIndex"]

enter image description here

To label two specific atoms

MoleculePlot3D[mol, 
 AtomLabels -> {1 -> Placed[Style["bob", 22], Center], 2 -> "alice"}]

enter image description here

AtomLabels will be documented starting in the next version. It's been extended so you can use atom patterns on the left hand side of the rules, and molecule properties on the right hand side, like

MoleculePlot3D[mol, 
 AtomLabels -> {Atom[Except["H"]] -> 
    MoleculeProperty["AtomicNumber"]}, AtomLabelStyle -> 16]

enter image description here

$\endgroup$
6
  • $\begingroup$ Very nice, thank you! The last question (I promise:)) is how to use superscripts and subscripts in AtomLabels -> {1 -> Placed[Style["bob", 22], Center], 2 -> "alice"}]? $\endgroup$
    – TobiR
    Nov 5, 2021 at 15:03
  • 1
    $\begingroup$ You can put any expression as the first argument to Placed, for example Style[Subscript["C", 1], 18, FontFamily -> "Times"] $\endgroup$
    – Jason B.
    Nov 5, 2021 at 15:08
  • 1
    $\begingroup$ You may find this useful: reference.wolfram.com/language/workflow/… $\endgroup$
    – Jason B.
    Nov 5, 2021 at 15:09
  • $\begingroup$ It is perfect! Thank you very much! $\endgroup$
    – TobiR
    Nov 5, 2021 at 15:18
  • 2
    $\begingroup$ Subsuperscript["C", 1, "\[Prime]"] $\endgroup$
    – Jason B.
    Nov 5, 2021 at 15:27

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.