How to write subscript and superscriopt in a single variable?

14 views (last 30 days)
Hello every one,
Can someone tell me how can i write this symbol in matlab script.
  2 Comments
John D'Errico
John D'Errico on 13 Mar 2020
Sorry. You cannot have a variable name with either subscripts or superscripts, so certainly not both. Are you asking how to use Latex to create something of the sort, perhaps in a published file or live script, but essentially as a comment?
If you insist on a variable name that contains that information, I might suggest that you insert underscore characters in the name. Thus, perhaps C_2_CH3OH.

Sign in to comment.

Answers (2)

Shlok
Shlok on 3 Dec 2024
Hi Vishnuvardhan,
To store a formula consisting of superscript and nested subscripts, the approach is to store it as a string with LaTeX formatting and use it with the LaTeX interpreter. Here is a sample code:
formula = '$C^{2}_{CH_3OH}$';
title(formula, 'Interpreter', 'latex');
In this example, ^ denotes superscript, and _ denotes subscript. Curly braces {} are used for grouping elements within the formula. Note that LaTeX-formatted strings can be viewed in the Live Editor and figures, but not in the console.
To know more about adding LaTeX formatting in Live Editor, refer to the following MathWorks documentation:

Walter Roberson
Walter Roberson on 3 Dec 2024
You can create symbolic variable names that are specially formatted. When expressions using those names are displayed in LiveScript (but not at the command window!) then the variables will be formatted.
Unfortunately, the formatting is restricted to one level of superscripts and subscripts, and to character ornations such as bars and hats. The formatting does not extend to being able to subscript the 3 on the while inside the construct. And the formatting unfortunately does not allow for --- after the base name, everything has to be subscript or superscript.
syms C__2_CH3OH
C__2_CH3OH
Sadly, at the moment the display of formatted symbolic output is broken, so at the moment you would not be able to see the output.

Categories

Find more on Environment and Settings in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!