latex output to .txt format

19 views (last 30 days)
Mohammadfarid ghasemi
Mohammadfarid ghasemi on 24 Jul 2021
Answered: Daniel Pusicha on 19 Jul 2022
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,

Answers (2)

Daniel Pusicha
Daniel Pusicha on 19 Jul 2022
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 on 24 Jul 2021
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 Comment
Daniel Pusicha
Daniel Pusicha on 19 Jul 2022
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.

Sign in to comment.

Categories

Find more on Characters and Strings in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!