Clear Filters
Clear Filters

How to create a table with interpreter latex?

12 views (last 30 days)
Sim
Sim on 22 May 2024
Answered: recent works on 22 May 2024
How to create a table with interpreter latex?
In the following example, I would like to have the text inside the table with interpreter latex:
dates = {'1$-$2 May 2024','2$-$3 May 2024','3$-$4 May 2024','4$-$5 May 2024','5$-$6 May 2024'};
for i = 1 : 5
a(i) = 2*i;
b(i) = sqrt(i);
c(i,:) = [{sprintf('$\\textbf{date: %s, class: %d}$',dates{i},i)} a(i) b(i)];
end
T = table(c(:,1),c(:,2),c(:,3),'VariableNames',{'date&class','a','b'})
T = 5x3 table
date&class a b _____________________________________________ ______ __________ {'$\textbf{date: 1$-$2 May 2024, class: 1}$'} {[ 2]} {[ 1]} {'$\textbf{date: 2$-$3 May 2024, class: 2}$'} {[ 4]} {[1.4142]} {'$\textbf{date: 3$-$4 May 2024, class: 3}$'} {[ 6]} {[1.7321]} {'$\textbf{date: 4$-$5 May 2024, class: 4}$'} {[ 8]} {[ 2]} {'$\textbf{date: 5$-$6 May 2024, class: 5}$'} {[10]} {[2.2361]}
How to do it?

Answers (1)

recent works
recent works on 22 May 2024
you can create the LaTeX code for the table
dates = {'1$-$2 May 2024','2$-$3 May 2024','3$-$4 May 2024','4$-$5 May 2024','5$-$6 May 2024'};
latex_table = '\\begin{tabular}{|c|c|c|}\n\\hline\n';
latex_table = [latex_table, 'date \\& class & a & b \\\\\n\\hline\n'];
for i = 1:5
a = 2*i;
b = sqrt(i);
latex_date_class = sprintf('\\textbf{date: %s, class: %d}', dates{i}, i);
latex_table = [latex_table, sprintf('$%s$ & %d & %.4f \\\\\n\\hline\n', latex_date_class, a, b)];
end
latex_table = [latex_table, '\\end{tabular}'];
disp(latex_table)
Example LaTeX Output:
Here's how the resulting LaTeX code would look when displayed in MATLAB's command window
\begin{tabular}{|c|c|c|}
\hline
date \& class & a & b \\
\hline
$\textbf{date: 1$-$2 May 2024, class: 1}$ & 2 & 1.0000 \\
\hline
$\textbf{date: 2$-$3 May 2024, class: 2}$ & 4 & 1.4142 \\
\hline
$\textbf{date: 3$-$4 May 2024, class: 3}$ & 6 & 1.7321 \\
\hline
$\textbf{date: 4$-$5 May 2024, class: 4}$ & 8 & 2.0000 \\
\hline
$\textbf{date: 5$-$6 May 2024, class: 5}$ & 10 & 2.2361 \\
\hline
\end{tabular}

Community Treasure Hunt

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

Start Hunting!