Látex ifFileExist

Látex ifFileExist
\subsection{Checksum: Factor, Specific, and Total Variance}
Perhaps the most basic test of a regression model is if the factor and specific variances sum to equal the total variance.  Here, we 
present this diagnostic as an xy-plot, with the sum of factor and specific variance on the x-axis and the total variance on the y-axis.  We 
expect the plotted points to line up tightly along the identity line.

\clearpage
\begin{figure}[!h]

\centering

\includegraphics{\Sexpr{image_dir}xyplotFacSpecTotVarTest.pdf}

\caption{Total Variance vs. Sum of Factor and Specific Variance, All Instruments Passing Exposure Model.}

\end{figure}

Pergunta:

Como faço para lidar com uma situação em que "xyplotFacSpecTotVarTest.pdf" não existe. Arquivo gerado a partir do conteúdo de uma tabela, que por sua vez pode ou não conter dados. Erros finais do processo de geração de PDF se estiver faltando "xyplotFacSpecTotVarTest.pdf" Como posso lidar com esse cenário? Eu tentei /IfFileExist mas não parece funcionar. Veja a implementação a seguir.

\IfFileExists{{image_dir}{xyplotFacSpecTotVarTest.pdf}} { 

\clearpage

\begin{figure}[!h]

\centering

\includegraphics{\Sexpr{image_dir}{xyplotFacSpecTotVarTest.pdf}}

\typeout{File Exist: xyplotFacSpecTotVarTest.pdf}

\caption{Total Variance vs. Sum of Factor and Specific Variance, All Instruments Passing Exposure Model.}

\end{figure} } 

{\typeout{File DOESNT Exist: xyplotFacSpecTotVarTest.pdf}}

Responder1

Olhando as tags, presumo que você use knitr. Por que não deixar o próprio R (silenciosamente) determinar se o arquivo existe?

<<chunkname,results='asis',echo=FALSE>>=
if (file.exists(file.path(image_dir, "xyplotFacSpecTotVarTest.pdf"))) {
   cat("\\clearpage\n")
   cat("\\begin{figure}[!h]\n")
   # and so on
}
else {
  cat("\\textbf{File does not exist.}\n\n")
}
@

Observe a results='asis'opção de pedaço. Cada catstring 'd será enviada para o .texarquivo como está (daí o nome da opção). Em outras palavras, cada catstring 'd será um comando LaTeX a ser compilado.

Você pode até generalizar o código acima criando uma função R sofisticada, por exemplo include_if_exists(filename), e reutilizá-la em vários pedaços.

informação relacionada