latex output to .txt format

19 ビュー (過去 30 日間)
Mohammadfarid ghasemi
Mohammadfarid ghasemi 2021 年 7 月 24 日
回答済み: Daniel Pusicha 2022 年 7 月 19 日
Hi,
I transfered some equations into latex and want to save the output as .txt format, can anyone tell me how can I do it?
thanks,

回答 (2 件)

Daniel Pusicha
Daniel Pusicha 2022 年 7 月 19 日
As Yongjian Feng suggested, the expression can be written to a .txt file using the fprint function. First we have to create a LaTeX character array from a symbolic expression:
syms test
formula = latex(test); % This produces the output '\mathrm{test}'
LaTeX expressions in general contain commands which are denoted by a backslash. However, the function that is used to write strings to .txt file uses the backslah for formatting. Replacing the backslash with a double backslash solves this problem:
formula = strrep(formula, '\', '\\');
Finally, the character array can be written to the file:
fid = fopen('test_file.txt','wt');
fprintf(fid, formula);
fclose(fid);

Yongjian Feng
Yongjian Feng 2021 年 7 月 24 日
So you convert some equations into latex as a string, right? Then you can just follow this to write the string into a .txt file:
https://www.mathworks.com/matlabcentral/answers/110573-write-string-in-text-file
  1 件のコメント
Daniel Pusicha
Daniel Pusicha 2022 年 7 月 19 日
I think there is at least one further step necessary as the latex expression will certainly contain some backslashes however fprintf() interprets a backslash as some formatting command.
syms test
formula = latex(test); % This produces the output '\mathrm{test}'
fid = fopen('test_file.txt','wt');
fprintf(fid, formula);
Warning: Escaped character '\m' is not valid. See 'doc sprintf' for supported special characters.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by