Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I know this may be extravagant, but I was wondering if it was possible to embed a terminal command in a beamer pdf that can be launch by clicking on it. My idea is that during my presentation, I could just click on a button displayed on my slide to launch a program. This way, I don't have to quit the presentation and open a terminal window.

I'm using mac OSX and I need the view the pdf with Adobe Reader. Is there a way to do it without using php forwarding like in here?

Edit:

I tried this code:

\begin{filecontents*}{shell.command}
#!/usr/bin/env sh
echo "Hello, World!"
\end{filecontents*}

\documentclass{beamer}

\begin{document}

\begin{frame}
    \href{run:shell.command}{here}
\end{frame}
\end{document}

And Adobe Reader says it can't find an app to open the file. I also tried opening the file with Safari and it shows the script file, it doesn't run it. This is annoying because if I click on the shell.command file through the Finder, it simply runs it, as it should.

share|improve this question
    
Thanks, I didn't saw that post. –  user2482876 Jul 24 '13 at 17:49
    
@Papiro The linked doesn't work in Evince at least, but mine does. –  Sean Allred Jul 24 '13 at 17:56
2  
Although this is technically a duplicate, both of the solutions seem only to work on Windows. I would prefer to repopen this an get a solution on the record that works with Linux/OS X. –  Alan Munn Jul 24 '13 at 20:22
    
@AlanMunn Agreed -- I'm thinking there might be a difference in how they each handle application calls. Perhaps someone can test it within Evince on Linux, since that worked on Windows? –  Sean Allred Jul 24 '13 at 22:38
add comment

2 Answers

Doing this in combination with listings gives you a pretty nice solution:

\documentclass{article}
\usepackage{filecontents,hyperref,listings}

\begin{filecontents*}{script.bat}
@echo off
echo "Hello, World!"
pause
\end{filecontents*}

\begin{document}
\lstinputlisting[language=csh,float,caption={A Windows batch file}]{script.bat}

Click \href{run:script.bat}{here} to run the script.
\end{document}

output

This should also work with Beamer.


Edit

If you're working with a UNIX system, note that bat is Windows-only, since Windows doesn't use a 'real' shell. With UNIX, give it a .sh extension (if you want to give it one at all). You might also need to do a few more things:

  • Make sure the file is executable: run chmod +x myscript.sh
  • Give the file a shebang line:

script.sh:

#!/usr/bin/env sh
echo "Oh, the wonderful possibilities of UNIX shells."

Please note that, as of now, this solution may not always work on Windows.

Note also that the handling of run: protocols can vary from viewer to viewer. Some may not support it at all due to security risks, but it has been confirmed that Evince and Skim support it.

share|improve this answer
    
I just tried that and adobe reader pops up an error saying it can't find a program to run shell.bat. –  user2482876 Jul 24 '13 at 17:57
    
@user2482876 Are you on Windows, or a UNIX variant (eg Mac)? If you're using UNIX, rename shell.bat to shell.sh; Windows uses the bat extension to indicate BATch scripts, where UNIX variants usually use sh to indicate SHell scripts. (Technically, the Bourne shell.) –  Sean Allred Jul 24 '13 at 17:58
    
ah! yes i'm on OSX. It makes way more sense! –  user2482876 Jul 24 '13 at 17:59
    
I tried again using .sh this time and it gave me the same error. Should I make an actual shell script file and use run:./shell.sh ? –  user2482876 Jul 24 '13 at 18:06
2  
@user2482876: It works with Skim, though (if the x bit is set). Apparently, every PDF viewer implements the run: handler differently on OS X :-( –  Daniel Jul 25 '13 at 15:47
show 9 more comments

Ok here's how to do it on OSX. Big thanks to everybody, especially to Sean Allred and Daniel.

Step 1

Your .tex file should follow this structure:

\begin{filecontents*}{shell.command}
#!/usr/bin/env sh
echo "This finally works!"
\end{filecontents*}

\documentclass{beamer}

\begin{document}

\begin{frame}
    \href{run:shell.command}{here}
\end{frame}
\end{document}

Using .command instead of .sh will make the script executable by the system.

Step 2

Go in terminal, cd into the directory and give the script the permission to run with:

chmod +x shell.command

Step 3

Use Skim to read the .pdf file because different readers seem to interpret differently the run: command.

share|improve this answer
    
I should note that Skim probably isn't the only one that works, but it has (obviously) been confirmed to do so. Evince works in Windows, so it will likely also work on OSX (and other *nix/bsd flavors). As a side note, isn't this basically the content in an existing answer? –  Sean Allred Jul 25 '13 at 16:22
    
@SeanAllred It seems that the crucial distinction is that the file must have the .command or the .tool extension. See What is the difference between .command, .tool, and .sh file extensions? –  Alan Munn Jul 25 '13 at 16:23
1  
@SeanAllred The issue is that since the run: command interfaces with the GUI part of the OS (not the underlying BSD part), it needs to execute a file that the GUI part of the OS understands, and for OS X this is files named .command or .tool, i.e. double clickable shell scripts. A regular .sh file is only executable from within the Terminal, but not by double clicking in the Finder, and run: behaves as if it's in the Finder. –  Alan Munn Jul 25 '13 at 16:30
1  
I did try using .sh and it opened the file in a text editor (Xcode by default on my computer). –  user2482876 Jul 25 '13 at 16:31
2  
Just to be clear for others who might encounter this answer, the filecontents part of the answer is not required; the .command file simply needs to be in the same folder as the .pdf file, and can be created in any way. –  Alan Munn Jul 25 '13 at 16:43
show 3 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.