ASKSAGE: Sage Q&A Forum - Individual question feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Fri, 13 Sep 2019 04:29:50 -0500How to Latex-print matrices as a bold uppercase letter?https://ask.sagemath.org/question/47770/how-to-latex-print-matrices-as-a-bold-uppercase-letter/Hello Sage users!
I was reading the documentation and trying to find a solution for my personal chimera of LaTeX usage: if I define $\mathbf A$ to be a matrix at some scope (document, chapter or section for example), why do I have to explictly write `\mathbf` every time?
So my question for Sage is this: say I have a simple 2x2 matrix
$$
\mathbf A = \left[
\begin{array}{rr}
a & b \\\\
c & d
\end{array}
\right]
$$
I want to, at times, do symbolic manipulations with the letter representation of this matrix, then use the `latex(...)` command to print it as a bold upcase letter, and to use the full matrix form at other times (with its corresponding LaTeX form).
Example of what I want:
sage: A = matrix(2,2,var('a', 'b', 'c', 'd'))
sage: latex(A)
\left(\begin{array}{rr}
a & b \\
c & d
\end{array}\right)
sage: latex(A.as_symbol())
\Bold{A}
Similarly, I would like it very much to perform symbolic calculations with A in sage defined as an "abstract matrix", i.e., a symbol of type matrix, that will render as a bold uppercase A (because is of type matrix) but will be used as a symbol in computations. Example.
sage: var('A', 'B', 'c')
sage: A,B :: AbstractMatrix
sage: latex(A)
\Bold{A}
sage: A*B
A*B
sage: (A+B)*c
A*c + B*c
sage: latex(A*B)
\Bold{A}\Bold{B}
sage: latex((A+B)*c)
\Bold{A}*c + \Bold{B}*c
Is it possible to do this in Sage? And how?Thu, 05 Sep 2019 04:02:34 -0500https://ask.sagemath.org/question/47770/how-to-latex-print-matrices-as-a-bold-uppercase-letter/Comment by adrber for <p>Hello Sage users!</p>
<p>I was reading the documentation and trying to find a solution for my personal chimera of LaTeX usage: if I define $\mathbf A$ to be a matrix at some scope (document, chapter or section for example), why do I have to explictly write <code>\mathbf</code> every time?</p>
<p>So my question for Sage is this: say I have a simple 2x2 matrix</p>
<p>$$
\mathbf A = \left[
\begin{array}{rr}
a & b \\
c & d
\end{array}
\right]
$$</p>
<p>I want to, at times, do symbolic manipulations with the letter representation of this matrix, then use the <code>latex(...)</code> command to print it as a bold upcase letter, and to use the full matrix form at other times (with its corresponding LaTeX form).</p>
<p>Example of what I want:</p>
<pre><code>sage: A = matrix(2,2,var('a', 'b', 'c', 'd'))
sage: latex(A)
\left(\begin{array}{rr}
a & b \\
c & d
\end{array}\right)
sage: latex(A.as_symbol())
\Bold{A}
</code></pre>
<p>Similarly, I would like it very much to perform symbolic calculations with A in sage defined as an "abstract matrix", i.e., a symbol of type matrix, that will render as a bold uppercase A (because is of type matrix) but will be used as a symbol in computations. Example.</p>
<pre><code>sage: var('A', 'B', 'c')
sage: A,B :: AbstractMatrix
sage: latex(A)
\Bold{A}
sage: A*B
A*B
sage: (A+B)*c
A*c + B*c
sage: latex(A*B)
\Bold{A}\Bold{B}
sage: latex((A+B)*c)
\Bold{A}*c + \Bold{B}*c
</code></pre>
<p>Is it possible to do this in Sage? And how?</p>
https://ask.sagemath.org/question/47770/how-to-latex-print-matrices-as-a-bold-uppercase-letter/?comment=47875#post-id-47875Added examples of what I meant.Fri, 13 Sep 2019 03:54:13 -0500https://ask.sagemath.org/question/47770/how-to-latex-print-matrices-as-a-bold-uppercase-letter/?comment=47875#post-id-47875Comment by rburing for <p>Hello Sage users!</p>
<p>I was reading the documentation and trying to find a solution for my personal chimera of LaTeX usage: if I define $\mathbf A$ to be a matrix at some scope (document, chapter or section for example), why do I have to explictly write <code>\mathbf</code> every time?</p>
<p>So my question for Sage is this: say I have a simple 2x2 matrix</p>
<p>$$
\mathbf A = \left[
\begin{array}{rr}
a & b \\
c & d
\end{array}
\right]
$$</p>
<p>I want to, at times, do symbolic manipulations with the letter representation of this matrix, then use the <code>latex(...)</code> command to print it as a bold upcase letter, and to use the full matrix form at other times (with its corresponding LaTeX form).</p>
<p>Example of what I want:</p>
<pre><code>sage: A = matrix(2,2,var('a', 'b', 'c', 'd'))
sage: latex(A)
\left(\begin{array}{rr}
a & b \\
c & d
\end{array}\right)
sage: latex(A.as_symbol())
\Bold{A}
</code></pre>
<p>Similarly, I would like it very much to perform symbolic calculations with A in sage defined as an "abstract matrix", i.e., a symbol of type matrix, that will render as a bold uppercase A (because is of type matrix) but will be used as a symbol in computations. Example.</p>
<pre><code>sage: var('A', 'B', 'c')
sage: A,B :: AbstractMatrix
sage: latex(A)
\Bold{A}
sage: A*B
A*B
sage: (A+B)*c
A*c + B*c
sage: latex(A*B)
\Bold{A}\Bold{B}
sage: latex((A+B)*c)
\Bold{A}*c + \Bold{B}*c
</code></pre>
<p>Is it possible to do this in Sage? And how?</p>
https://ask.sagemath.org/question/47770/how-to-latex-print-matrices-as-a-bold-uppercase-letter/?comment=47782#post-id-47782How do you LaTeX it without bold? Please add sample code.Sat, 07 Sep 2019 06:48:08 -0500https://ask.sagemath.org/question/47770/how-to-latex-print-matrices-as-a-bold-uppercase-letter/?comment=47782#post-id-47782Answer by tmonteil for <p>Hello Sage users!</p>
<p>I was reading the documentation and trying to find a solution for my personal chimera of LaTeX usage: if I define $\mathbf A$ to be a matrix at some scope (document, chapter or section for example), why do I have to explictly write <code>\mathbf</code> every time?</p>
<p>So my question for Sage is this: say I have a simple 2x2 matrix</p>
<p>$$
\mathbf A = \left[
\begin{array}{rr}
a & b \\
c & d
\end{array}
\right]
$$</p>
<p>I want to, at times, do symbolic manipulations with the letter representation of this matrix, then use the <code>latex(...)</code> command to print it as a bold upcase letter, and to use the full matrix form at other times (with its corresponding LaTeX form).</p>
<p>Example of what I want:</p>
<pre><code>sage: A = matrix(2,2,var('a', 'b', 'c', 'd'))
sage: latex(A)
\left(\begin{array}{rr}
a & b \\
c & d
\end{array}\right)
sage: latex(A.as_symbol())
\Bold{A}
</code></pre>
<p>Similarly, I would like it very much to perform symbolic calculations with A in sage defined as an "abstract matrix", i.e., a symbol of type matrix, that will render as a bold uppercase A (because is of type matrix) but will be used as a symbol in computations. Example.</p>
<pre><code>sage: var('A', 'B', 'c')
sage: A,B :: AbstractMatrix
sage: latex(A)
\Bold{A}
sage: A*B
A*B
sage: (A+B)*c
A*c + B*c
sage: latex(A*B)
\Bold{A}\Bold{B}
sage: latex((A+B)*c)
\Bold{A}*c + \Bold{B}*c
</code></pre>
<p>Is it possible to do this in Sage? And how?</p>
https://ask.sagemath.org/question/47770/how-to-latex-print-matrices-as-a-bold-uppercase-letter/?answer=47877#post-id-47877Ragarding the first question, you should understand that when you write:
sage: A = matrix(2,2,var('a', 'b', 'c', 'd'))
`A` is a *Python name*, that is a kind of pointer to the matrix object, but the matrix object doesn't know anything about which names point to it. For example, you can then do:
sage: B = A
Here `B` is just another Python name pointing to the very same object in memory as you can check with:
sage: B is A
True
So, if you want to type `A.as_symbol()`, there is nowhere in the matrix object that registered the fact that the matrix has "A" as a string representation.
We can define a function that finds one of the Python names of the matrix object within the `globals()` dictionary and pick the first one that appears (and that is not ugly like `_42`), but it is kind of artificial, like:
sage: def sym(a):
....: for i in globals():
....: if not i.startswith('_') and globals()[i] is a:
....: return SR.var(i, latex_name="\Bold{{{}}}".format(i))
You have:
sage: latex(sym(A))
{\Bold{A}}
But of course:
sage: latex(sym(B))
{\Bold{A}}
Regarding the second question, it is a bit different, since the symbolic variable knows a string reprentation of itself, and you can even tune a latex one as follows:
sage: A = SR.var('A', latex_name="\Bold{A}")
sage: B = SR.var('B', latex_name="\Bold{B}")
sage: c = SR.var('c')
sage: A*B
A*B
sage: latex(A*B)
{\Bold{A}} {\Bold{B}}
sage: e = (A+B)*c
sage: e
(A + B)*c
sage: e.expand()
A*c + B*c
sage: latex(e.expand())
{\Bold{A}} c + {\Bold{B}} cFri, 13 Sep 2019 04:29:50 -0500https://ask.sagemath.org/question/47770/how-to-latex-print-matrices-as-a-bold-uppercase-letter/?answer=47877#post-id-47877