#42 Feature Request: Carry additional files to temp dir

v1.0_(example)
open
nobody
None
5
2014-04-10
2014-03-19
Daniel Fett
No

[I really hope that I'm not annoying you with my feature requests. If so, please let me know!]

When writing LaTeX code, one might want to use external files that reside within the directory of the presentation. For example, in the preamble, one could use:
\usepackage{mystyle}
Where mystyle.sty is a file that sits in the presentation's directory.

Currently, TexMaths cannot handle this, as the LaTeX code is moved into a new directory, so relative paths break. Absolute paths are not an option in this use case.

Therefore I provide the following patch, which collects all local files referenced in \usepackage, \input and \include commands, and copies them to the temp dir before running LaTeX. It works for files referenced in the preamble as well as those referenced somewhere else.

In line 481 of TexMathsEquations (after "oTextStream...") add:

' Check if the latex code has any dependencies that can be fulfilled easily by copying files from source dir to latex dir
' We check for include and input (with .tex files) and usepackage (with .sty files)
' Note that we search in the preamble and in the latex code (we concatenate the two)
Dim sPotentialFile as String
Dim sFilePath as String

Dim aSplittedURL() as String
Dim sCurrentFilePath as String
aSplittedURL = split(ThisComponent.getURL(), "/")
sCurrentFilePath = Left(ThisComponent.getURL(), Len(ThisComponent.getURL()) - Len(aSplittedUrl(UBound(aSplittedUrl))))

For Each sPotentialFile in FindInLatexCommand(glb_sPreamble & sLatexCode, "include")
    sFilePath = sCurrentFilePath & "/" & sPotentialFile & ".tex"
    If oFileAccess.exists(sFilePath) Then
        oFileAccess.copy(sFilePath, ConvertToURL( glb_sTmpPath & sPotentialFile & ".tex"))
    End If
Next

For Each sPotentialFile in FindInLatexCommand(glb_sPreamble & sLatexCode, "input")
    sFilePath = sCurrentFilePath & "/" & sPotentialFile & ".tex"
    If oFileAccess.exists(sFilePath) Then
        oFileAccess.copy(sFilePath, ConvertToURL( glb_sTmpPath & sPotentialFile & ".tex"))
    End If
Next

For Each sPotentialFile in FindInLatexCommand(glb_sPreamble & sLatexCode, "usepackage")
    sFilePath = sCurrentFilePath & "/" & sPotentialFile & ".sty"
    If oFileAccess.exists(sFilePath) Then
        oFileAccess.copy(sFilePath, ConvertToURL( glb_sTmpPath & sPotentialFile & ".sty"))
    End If
Next

Add the following Function to TexMathsTools:

' Return all strings that are contained in a given LaTeX command.
' For example, for the source code "\test{a} b c" and the command (search string) "test", it will return an array with one value, namely "a".
' Author: Daniel Fett
Function FindInLatexCommand(sSourceCode as String, sCommand as String)
Dim aStrings() As String
Dim sSearch As String
Dim iLast As Integer
Dim iCommandStart As Integer
iCommandStart = 1
iLast = 1
sSearch = "\" & sCommand & "{"

Do While iLast <> 0 and iCommandStart <> 0
    iCommandStart = InStr(iLast, sSourceCode, sSearch)
    If iCommandStart <> 0 Then
        ' We found the beginning of the command. Great, now search for the end!
        iLast = InStr(iCommandStart, sSourceCode, "}")
        ReDim Preserve aStrings(UBound(aStrings) + 1)
        aStrings(UBound(aStrings)) = Mid(sSourceCode, iCommandStart + Len(sSearch), iLast - (iCommandStart + Len(sSearch)))
    End If
Loop
FindInLatexCommand = aStrings

End Function

The same code is also contained in the attachments.

2 Attachments

Discussion

  • Daniel Fett
    Daniel Fett
    2014-03-20

    One should also add a 'On Error Goto SkipCopyingFiles' before the part in TexMathsEquations, and the mark SkipCopyingFiles afterwards. This prevents a bug when the document has not been saved yet.

    So the new patch is:

    On Error Goto SkipCopyingFiles
        Dim aSplittedURL() as String
        Dim sCurrentFilePath as String
        aSplittedURL = split(ThisComponent.getURL(), "/")
        sCurrentFilePath = Left(ThisComponent.getURL(), Len(ThisComponent.getURL()) - Len(aSplittedUrl(UBound(aSplittedUrl))))
    
        For Each sPotentialFile in FindInLatexCommand(glb_sPreamble & sLatexCode, "include")
            sFilePath = sCurrentFilePath & "/" & sPotentialFile & ".tex"
            If oFileAccess.exists(sFilePath) Then
                oFileAccess.copy(sFilePath, ConvertToURL( glb_sTmpPath & sPotentialFile & ".tex"))
            End If
        Next
    
        For Each sPotentialFile in FindInLatexCommand(glb_sPreamble & sLatexCode, "input")
            sFilePath = sCurrentFilePath & "/" & sPotentialFile & ".tex"
            If oFileAccess.exists(sFilePath) Then
                oFileAccess.copy(sFilePath, ConvertToURL( glb_sTmpPath & sPotentialFile & ".tex"))
            End If
        Next
    
        For Each sPotentialFile in FindInLatexCommand(glb_sPreamble & sLatexCode, "usepackage")
            sFilePath = sCurrentFilePath & "/" & sPotentialFile & ".sty"
            If oFileAccess.exists(sFilePath) Then
                oFileAccess.copy(sFilePath, ConvertToURL( glb_sTmpPath & sPotentialFile & ".sty"))
            End If
        Next
    

    SkipCopyingFiles:

     
  • Roland Baudin
    Roland Baudin
    2014-03-24

    I've started to integrate your patch, but it would be nice if you could provide a test case. For example, a test case with \usepackage in the preamble and one with \include (or \input) would be useful...
    Thanks,
    RB

     
  • Daniel Fett
    Daniel Fett
    2014-03-25

    Thanks for your efforts! Here is a test case. You should be able to recompile all the Texmaths-Equations if the patch was successfully integrated.

     
  • Roland Baudin
    Roland Baudin
    2014-04-09

    When you work with a new document (not already saved somewhere), if you use an \include command, then the macro will fail. This is why I added a test to prevent this.
    RB

     
  • Daniel Fett
    Daniel Fett
    2014-04-09

    I see that, but only for the usepackage case, you had the following code executed when the file was not saved yet:

    sFilePath = ConvertToURL( glb_sTmpPath & sPotentialFile & ".sty") ' Path of sty file in tmp dir
    If oFileAccess.exists(sFilePath) Then
    oFileAccess.kill(sFilePath)
    End If

    Where for the other cases (include and input), a msgbox was generated. The patch uses the msgbox for all three cases

     
  • Roland Baudin
    Roland Baudin
    2014-04-10

    Let me explain the rationale for these lines:

    1. When the document is not saved and an \include command or an \input command are used, TexMaths crashed. So I inserted the test If sUrl = "" and displayed a msgbox. This prevents a crash.

    2. But when the document is not saved and an \usepackage command is used, TexMaths does not crash (and an explicit LaTeX error message is displayed). However, if there is already an old .sty file with the same name in the tmp dir, it is used. So I use the test If sUrl = "" and delete the old .sty file. There is no need for a msgbox in this case.

    Of course, your solution is also valid...
    RB

     
  • Daniel Fett
    Daniel Fett
    2014-04-10

    Okay, that sounds very reasonable. This feature request can be closed, I think? Again, thanks for your work! In a few weeks, you will have 7 new TexMaths users :-)

     
  • Roland Baudin
    Roland Baudin
    2014-04-10

    Nice!
    RB