How to publish symbolic matrices in latex format in MATLAB?

43 views (last 30 days)
Elie Hatem
Elie Hatem on 2 Jun 2020
Edited: Elie Hatem on 2 Jun 2020
I have a project in which I have to publush symbolic matrices in latex format.
An example of a matrix is the following:
J1 =
[ 1, 0, a]
[ 1, 0, -a]
[ cos(beta_3s), sin(beta_3s), b*sin(beta_3s)]
If I try to publish it in latex format, I get the same eqiation as above:
\color{lightgray} \begin{verbatim}
J1 =
[ 1, 0, a]
[ 1, 0, -a]
[ cos(beta_3s), sin(beta_3s), b*sin(beta_3s)]
\end{verbatim} \color{black}
As you can see, the matrix is not publlished in the neat way that latex can actually write the matrix.
Moreover, if I tried solving the problem using a function that I created as follows:
function lat = tex(input)
This outputs the desired code which is: '$\left(\begin{array}{ccc} -\sin\left(\beta _{\mathrm{1s}}\right) & \cos\left(\beta _{\mathrm{1s}}\right) & b\,\sin\left(\beta _{\mathrm{1s}}\right)\\ \sin\left(\beta _{\mathrm{2s}}\right) & -\cos\left(\beta _{\mathrm{2s}}\right) & b\,\sin\left(\beta _{\mathrm{2s}}\right)\\ -\sin\left(\beta _{\mathrm{3c}}\right) & \cos\left(\beta _{\mathrm{3c}}\right) & c\,\sin\left(\beta _{\mathrm{3c}}\right) \end{array}\right)$'
However, if I try to publish the script in latex format using the function above and the disp function, (disp(tex(J1))), I get the following output in the published .tex file:
\color{lightgray} \begin{verbatim}$\left(\begin{array}{ccc} 1 & 0 & a\\ 1 & 0 & -a\\ \cos\left(\beta _{\mathrm{3s}}\right) & \sin\left(\beta _{\mathrm{3s}}\right) & b\,\sin\left(\beta _{\mathrm{3s}}\right) \end{array}\right)$
\end{verbatim} \color{black}
So as you can see, the code is now published with \begin{verbatim} \end{verbatim} . So, instead of showing the matrix, the latex code will show the code instead.
Is there a way to output the latex code in a way that it will be outside \begin{verbatim} \end{verbatim} and make it display the symbolic matrix?
I am sorry for the long question but I saw some people tried to ask the same question but did not find an answer. So, I tried to detail my problem as much as possible.
Thank you for your time.

Accepted Answer

Ameer Hamza
Ameer Hamza on 2 Jun 2020
You may try Live scripts. It renders the symbolic matrices in mathematical notation and allows you to get latex code directly. For example, run this code in Live script
syms a beta_3s b
x = [1 0 a;
1 0 -a;
cos(beta_3s) sin(beta_3s) b*sin(beta_3s)]
right-click on the output and click "copy as Latex". It gives the following latex code
1 & 0 & a\\
1 & 0 & -a\\
\mathrm{cos}\left(\beta_{\textrm{3s}} \right) & \mathrm{sin}\left(\beta_{\textrm{3s}} \right) & b\,\mathrm{sin}\left(\beta_{\textrm{3s}} \right)
which is rendered like this
  1 Comment
Elie Hatem
Elie Hatem on 2 Jun 2020
Thank you so much for your answer. The main issue is that the user of the final code will be able to choose a .txt table. Then, from that table, all the computations will be done and published to a .tex file . So, everything should be done automaticallly and each time, the table may change. An example of such a table is attached above. Moreover, please note that the beta angles are not inputed by the user, but generated automatically by MATLAB based on the first column of the table (if there is a 'f' in the first column of the stable, this means that the robot has a fixed wheel, and the beta angle for that wheel will be null. However, if there is a 's' or a 'c', this means that the wheel is a steerable wheel or castor wheel respectively and a symbolic beta angle will be generated in both of these cases)
Thank you

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!