In julia you can write subscripts by \_
for variable names. I was wondering if there is anything similar for writing fractions in variable names. Something like \frac{}{}
in LaTeX. I understand this may be harder as it takes two arguments. If there is none, I will use /
. But in this case I would like to use some enclosures to make clear what is being differentiated. I assume ()
is not usable? []
or {}
would be ok?

- 9,608
- 6
- 34
- 56

- 401
- 5
- 11
-
I guess `/` is probably not usable either... – ztyh Oct 15 '18 at 16:59
-
I guess I can use `\div` – ztyh Oct 15 '18 at 17:02
-
can also use `\llbracket` etc, but now its too long defeats the purpose... – ztyh Oct 15 '18 at 17:11
-
but i guess its just not possible to fit too much vertically in a single line... – ztyh Oct 15 '18 at 17:12
-
just to let you know, I really cherish the freedom in variable naming. Naming variables is becoming a real headache – ztyh Oct 15 '18 at 17:20
-
`\not` is probably better than `\div` – ztyh Oct 15 '18 at 17:43
2 Answers
The subscripts or other non-latin names you see in Julia code are just normal unicodes the same as "regular" names. the LaTeX commands are only a function of Julia REPL to remember and input them.
As for unicode, in principle you can represent some simple fractions like ⁽²⁺ⁱ⁾⁄₍ₛ₊ₜ₎
, using the ⁄
(U+2044 Fraction slash) symbol and subscripts and superscripts. The rendering depends on your font, but do not expect a verticle layout in any current fonts.
However, Julia recognizes ⁄
(U+2044 Fraction slash, not the /
in your keyboard) as "invalid character" when used along during parsing. The same applies to \not
, which can only be used in conjunction with some operators, so it's not an option too.
As for the brackets and the normal /
, they are operators and are parsed differently. However, there is an (ugly) way to circumvent this: you can use macros to bypass the parsing and use strings as variable names. For example:
julia> macro n_str(name)
esc(Symbol(name))
end
@n_str (macro with 1 method)
julia> n"∂(2x + 3)/∂x" = 2
2
julia> 2n"∂(2x + 3)/∂x"
4

- 2,836
- 13
- 25
-
Hi thank you for the detailed response. Sorry I'm actually not familiar with macros. So how was `n_str` used? – ztyh Oct 17 '18 at 03:49
-
Hi, just like in the example, you cound use `n"anything"` as a variable, so the allowed characters extend to anything that can appear in a string, at the cost of `n""` around the name. [this is the document](https://docs.julialang.org/en/v1/manual/metaprogramming/index.html#Non-Standard-String-Literals-1) for non-standard string literals, and you might need to read the whole page to fully understand how macro works. – 张实唯 Oct 17 '18 at 04:18
I think you might be looking for \diagup (⁄
).

- 1
- 1
- 1
-
This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/late-answers/34831524) – Ram Chander Aug 18 '23 at 12:45