TransWikia.com

How to create some macros

Mathematica Asked by cyrille.piatecki on December 24, 2020

I have two question in one. In the following code, I want to manipulate a plot in a centered cell. It is fastiduous to try always to remember the way to act. Could I define some macro as in a standard editor to keep some line of code pre typed ?

CellPrint[
 ExpressionCell[
  Manipulate[
   Plot[{n^2, n^3}, {n, 0, 1}, 
    PlotRange -> {{0, 1}, {-100, 300}}, 
    AxesLabel -> {"n", "Wh[n], Wc[n]"}, 
    PlotStyle -> {, Dashed, , Dashed}
   ],
   {{[Xi], 1}, 0, 1, 0.0001, Appearance -> "Labeled"}, 
   {{Kh, 3}, 0, 20, 0.0001, Appearance -> "Labeled"}, 
   ControlPlacement -> Left
  ], 
 "Output", TextAlignment -> Center
 ]
]

2 Answers

You can also use Style as follows:

centerF = Style[#, TextAlignment -> Center] &;

centerF@Manipulate[Plot[{n^2, n^3}, {n, 0, 100}, PlotRange -> {{0, 1}, {-1, 1}}, 
   AxesLabel -> {"n", "Wh[n], Wc[n]"}, 
   PlotStyle -> {Dashed, Dashed}], {{[Xi], 1}, 0, 1, 0.0001, 
   Appearance -> "Labeled"}, {{Kh, 3}, 0, 20, 0.0001, 
   Appearance -> "Labeled"}, ControlPlacement -> Left]

enter image description here

Correct answer by kglr on December 24, 2020

You might define a centering function to shorten your notations. On the basis of your code it will be

 center[x_] := 
  CellPrint[ExpressionCell[x, "Output", TextAlignment -> Center]];

Then your code will look like the following:

 Manipulate[
  Plot[{a*n^2, b*n^3}, {n, 0, 1}, PlotRange -> {{0, 1}, {-100, 300}}, 
   AxesLabel -> {"n", "Wh[n], Wc[n]"}, 
   PlotStyle -> {Dashed, , Dashed}], {{a, 1}, 0, 1, 0.0001, 
   Appearance -> "Labeled"}, {{b, 3}, 0, 20, 0.0001, 
   Appearance -> "Labeled"}, ControlPlacement -> Top] // center

yielding this:

enter image description here

The drawback is that you will need to first evaluate the function centerin the beginning of every session. The ways around that I see would be to either create a StyleSheet as I mentioned in the comment above, or to pack this function into a package and to define it such that it opens together with Mma.

Have fun!

Answered by Alexei Boulbitch on December 24, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP