Hey everyone! I'm looking for some help with a script I'm trying to create that would manage my .tex files. I want it to open a terminal and run the command `latexmk -pdf -pvc $FILE`, where `$FILE` is the specific .tex file I want to compile. Additionally, I want the script to open the same file in KWrite. Ideally, I'd like to set this script up as a default application for .tex files, so when I double-click on any .tex file, both actions happen automatically.
I started with a basic script named `latex.sh`:
```bash
#!/bin/bash
latexmk -pdf -pvc $1 &
kwrite $1 &
```
Then, I created a corresponding `.desktop` file to facilitate linking it as an application. However, I'm running into issues getting this to work. Can anyone point me in the right direction? Thanks!
P.S. I found a solution later which included using a proper .desktop file and modifying my script a bit, but I'm curious if there's a better way to set this up!
3 Answers
Hey! You’re in the right place for this! First, I’d suggest wrapping `$1` in quotes to handle paths with spaces properly. For your `.desktop` entry, don't forget to add `%u` at the end of your `Exec` line to pass the file as an argument to your script.
Try running your `latexmk` in a new terminal window with something like:
```bash
xterm -e latexmk -pdf -pvc "$1"; read
```
This keeps the terminal open for you to see the logs. Alternatively, using `konsole` would work too, just keep in mind the window behavior after execution. If your terminal closes too fast, consider adding a `read` command to wait for user input before it exits.
Here’s an example that might work for you:
```bash
#!/usr/bin/env bash
kwrite "$1" &
xterm -e 'latexmk -pdf -pvc "$1"; read' &
```
And for the `.desktop` file:
```bash
[Desktop Entry]
Type=Application
Name=Latex
Exec=/home/user/latex.sh %u
```
Wow, I didn't know about the `%u` flag for `.desktop` entries! Thanks for that tip!
You’re on the right track! Just a heads up: make sure that your script is executable with `chmod +x latex.sh`. This can prevent a lot of headaches. And remember to use `%u` in your .desktop file to make sure your script receives the file argument properly.
After making those adjustments, it ran perfectly for me—no issues at all!
I tried something similar, and it worked fine using `konsole` for the terminal. When you include the `-pvc` option in your `latexmk` command, it keeps the terminal session alive until you close it yourself, which is convenient! Your script looks good, just ensure that the path in your `.desktop` file points correctly to your script.
Here’s how my final setup looked:
```bash
#!/bin/bash
kwrite "$1" &
konsole -e latexmk -pdf -pvc "$1" &
```
And here’s my .desktop entry:
```bash
[Desktop Entry]
Encoding=UTF-8
Version=1.0
Type=Application
Terminal=false
Exec=/home/user/latex.sh %u
Name=Latex
```
Everything runs smoothly on my end after those adjustments!
That’s a great breakdown! Love how clear it is. I’ve been looking to do something similar, so I’ll definitely give that a try.