필터 지우기
필터 지우기

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 CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by