Compile Latex Code Using Swift

Compile Latex code using Swift

The launchPath must be set to the path of the executable,
for example

task.launchPath = "/usr/texbin/latexmk"

The currentDirectoryPath can optionally be set to execute the
task in a specified directory. The "Documents" directory
is usually determined like this:

task.currentDirectoryPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as NSString

Finally, the arguments are the command line arguments for
the executable, for example

task.arguments = ["-xelatex", file]

Alternatively, you can start the executable using the shell,
something like

task.launchPath = "/bin/sh"
task.currentDirectoryPath = ...
task.arguments = ["-c", "latexmk -xelatex \"\(file)\""]

The advantage is that the shell uses the PATH environment variable to
locate the executable. One disadvantage is that quoting the arguments
correctly is more difficult.

Update: It seems that "/usr/texbin" must be in the PATH for the
LaTeX process. This can be done as follows:

// Get current environment:
var env = NSProcessInfo.processInfo().environment
// Get PATH:
var path = env["PATH"] as String
// Prepend "/usr/texbin":
path = "/usr/texbin:" + path
// Put back to environment:
env["PATH"] = path
// And use this as environment for the task:
task.environment = env

How to embed Latex in iOS, Objective C?

Since I have no idea what your specific need is, the easiest way is to compile into postscript or PDF and include that. You can also use Texify if your app uses internet connectivity.

If you mean you want to write an app like TeX Touch, then you'll have to narrow down the question a bit.

Compile C code and expose it to Swift under Linux

If you build a library out of your C code, you can create a system module for it, which can then be imported into Swift, see this answer: Use a C library in Swift on Linux.

Another way to approach this task is to create a bridging header, as suggested by @Philip. Here is an oversimplified example. Let's consider the following C code:

/* In car.h */
int getInt();

/* In car.c */
int getInt() { return 123; }

We will use car.h as the bridging header. The swift source is (in file junk.swift):

print("Hi from swift!")
var i = getInt()
print("And here is an int from C: \(i)!")

First, create an object file, car.o, from car.c:

gcc -c car.c

Now build an executable, junk, as follows:

swiftc -import-objc-header car.h junk.swift car.o -o junk

Running the executable gives:

$ ./junk
Hi from swift!
And here is an int from C: 123!

The -import-objc-header option is hidden. To see it and a bunch of other hidden options, run:

swiftc -help-hidden 

I did this using Swift 3.0 development snapshot for Ubuntu 14.04 from April 12, available here: https://swift.org/builds/development/ubuntu1404/swift-DEVELOPMENT-SNAPSHOT-2016-04-12-a/swift-DEVELOPMENT-SNAPSHOT-2016-04-12-a-ubuntu14.04.tar.gz

Now, if you want to use C++, you will need to create a wrapper, written in a C++ source file and compiled with a C++ compiler, but with functions callable from C by using extern "C". Those functions can then be called from Swift as any C function. See, for example, this answer: Can I mix Swift with C++? Like the Objective - C .mm files

Is it possible to code in Swift and deploy to real life devices on Windows?

Right now Swift is only for MacOS, iOS and linux devices available. While there are unofficial ports for windows like this one: Swift for Windows and Robert mentioned. The best solution right now to have the latest version of swift and run it is to buy either a Mac or an iOS device or an ubuntu device.

Why does this Swift code snippet compile? How does it work?

What you are actually doing is calling the method .contains(predicate: String -> Bool) (the actual method can throw, but that's not relevant here)

This means that you are asking the array colours if it contains an element that conforms to that predicate, which is "The red one.".containsString. So the array is checking its elements one by one and checking it against that predicate. If it finds one, it will return true, otherwise it will return false.

The code above does this:

"The red one.".containsString("red")
"The red one.".containsString("green")
"The red one.".containsString("blue")

"The yellow one.".containsString("red")
"The yellow one.".containsString("green")
"The yellow one.".containsString("blue")

And it checks if it got true somewhere.

GM release of Xcode 6 compile

This error can happen for numerous reasons, so this is meant to be a debugging hint. You may want to try using xcodebuild in the command line. It will give you details as to what files are the culprits.

To do this, open Terminal and go to your project folder. Once there, type in

xcodebuild -project YourProject.xcodeproj -scheme YourScheme

or if you're working in a workspace

xcodebuild -workspace YourProject.xcworkspace -scheme YourScheme

You may see A LOT of messages pop up, but at the very end of the output you should see the specific files that are causing the crash. Back in XCode, go into those files and start playing with some of the Swift syntax to see what's going on. In my case, it had to do with the setAttributeString function, but I've seen other people have issues with ! and ?.

Hopefully that will get you headed in the right direction.



Related Topics



Leave a reply



Submit