Using Matlab to create a LaTex document (for image import)

조회 수: 29 (최근 30일)
Niklas Kurz
Niklas Kurz 2022년 4월 27일
댓글: Niklas Kurz 2022년 4월 28일
In order to import multiple figures in LaTex from one folder I have found following code:
fileID = fopen('./incl_img_latex.txt', 'w');
files = dir('path');
for file = files'
str = file.name;
fprintf(fileID, '\\begin{figure}[htb!]\n\\centering\n\\setlength{\\fboxsep}{1pt}\n\\setlength{\\fboxrule}{1pt}\n\\fbox{\\includegraphics[width = 0.95\\textwidth]{%s}}\n\\caption{}\\end{figure}\n \n', str);
end
fclose(fileID);
This will create for each file some LaTeX code:
\begin{figure}[htb!]
\centering
\setlength{\fboxsep}{1pt}
\setlength{\fboxrule}{1pt}
\fbox{\includegraphics[width = 0.95\textwidth]{IMG_0252.jpg}}
\end{figure}
\begin{figure}[htb!]
\centering
\setlength{\fboxsep}{1pt}
\setlength{\fboxrule}{1pt}
\fbox{\includegraphics[width = 0.95\textwidth]{IMG_0253.jpg}}
\end{figure}
\begin{figure}[htb!]
\centering
\setlength{\fboxsep}{1pt}
\setlength{\fboxrule}{1pt}
\fbox{\includegraphics[width = 0.95\textwidth]{IMG_0254.jpg}}
\end{figure}
\begin{figure}[htb!]
\centering
\setlength{\fboxsep}{1pt}
\setlength{\fboxrule}{1pt}
\fbox{\includegraphics[width = 0.95\textwidth]{IMG_0255.jpg}}
\end{figure}
Now here comes the tricky thing that I want to implement: For each second file the Latex code should vary:
fprintf(fileID, '\\begin{figure}[htb!]\n\\centering\n\\setlength{\\fboxsep}{1pt}\n\\setlength{\\fboxrule}{1pt}\n\\fbox{\\includegraphics[width = 0.95\\textwidth]{%s}}\n\\caption{}\\end{figure}\n \\clearpage \n \n', str);
So something like that is received:
\begin{figure}[htb!]
\centering
\setlength{\fboxsep}{1pt}
\setlength{\fboxrule}{1pt}
\fbox{\includegraphics[width = 0.95\textwidth]{IMG_0252.jpg}}
\end{figure}
\begin{figure}[htb!]
\centering
\setlength{\fboxsep}{1pt}
\setlength{\fboxrule}{1pt}
\fbox{\includegraphics[width = 0.95\textwidth]{IMG_0253.jpg}}
\end{figure}
\clearpage %this Line is new
\begin{figure}[htb!]
\centering
\setlength{\fboxsep}{1pt}
\setlength{\fboxrule}{1pt}
\fbox{\includegraphics[width = 0.95\textwidth]{IMG_0254.jpg}}
\end{figure}
\begin{figure}[htb!]
\centering
\setlength{\fboxsep}{1pt}
\setlength{\fboxrule}{1pt}
\fbox{\includegraphics[width = 0.95\textwidth]{IMG_0255.jpg}}
\end{figure}
\clearpage %this Line is new
How do I achieve this?

채택된 답변

Walter Roberson
Walter Roberson 2022년 4월 27일
편집: Walter Roberson 2022년 4월 27일
fileID = fopen('./incl_img_latex.txt', 'w');
files = dir('path');
files([files.isfolder]) = []; %remove . and .. and any directories
for fileidx = 1 : numel(files)
file = files(fileidx);
str = file.name;
fprintf(fileID, '\\begin{figure}[htb!]\n\\centering\n\\setlength{\\fboxsep}{1pt}\n\\setlength{\\fboxrule}{1pt}\n\\fbox{\\includegraphics[width = 0.95\\textwidth]{%s}}\n\\caption{}\\end{figure}\n \n', str);
if mod(fileidx,2) == 0
fprintf(fileID, '\\clearpage\n');
end
end
fclose(fileID);

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 LaTeX에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by