Alexander Grahn
Version 1.06, 2nd May 2007
Copyright © 2006 Alexander Grahn
This HOWTO provides instructions for setting up a portable installation
of MiKTeX (>=2.5), Ghostscript and GSview on external storage devices,
such as USB sticks or mapped network drives, for use with MS Windows
(2000, 2003, XP, ...) computers.
Permission is granted to copy, distribute and/or modify this HOWTO
under the terms of the GNU Free Documentation License (GFDL), Version
1.1 or any later version published by the Free Software Foundation. See
http://www.fsf.org for details of this license. This HOWTO is distributed
in the hope that it will be useful, but WITHOUT ANY WARRANTY.
-----------------------------------------------------------------------------
1 Introduction
==============
A portable TeX/LaTeX/PS-viewer installation for MS Windows can be
easily created by copying existing installations of MiKTeX, Ghostscript
and GSview to a USB storage device and adding some helper scripts
for initialization and utilizing the software. In fact, the procedure
described here is not restricted to USB drives but works with everything,
which, apart from being writable, has a leading drive letter, such as
"E:", in its path, e. g. mapped network drives.
As Ghostscript (together with the PS fonts directory) and GSview are
quite small (they consume less than 30 MB), the required size of the USB
drive mainly depends on the number of packages that have been or will
be installed with the MiKTeX system. The MiKTeX Package Manager which
ships with the MiKTeX system allows to update or extend the portable
installation at any time the USB drive is plugged into a Windows box
with internet connection. A script for starting the Package Manager is
provided within this document.
The initialization of the portable installation involves some manipulation
of the Windows Registry in order to associate file name extensions
(.dvi, .ps, .eps) with the appropriate programs (Yap, GSview) of the
installation. These changes only affect the current user's settings
branch (HKEY_CURRENT_USER) of the Registry. Therefore, no additional
administrative rights are necessary to run the initialization script
and to use the software. Existing application settings as well as file
type associations are backed up and should be restored after finishing
the session using another script given in this document.
The installation of other software which might complete the portable
system, such as text editors, additional viewers, drawing programs etc. is
not covered here as it depends largely on personal requirements and taste.
2 Prerequisites
===============
First of all, we need working installations of Ghostscript, GSview
and MiKTeX on some Windows box. At the beginning, a small installation
(private or shared) of MiKTeX will do, since it can be extended later
using the Package Manager.
Create two empty folders `texmf' and `localtexmf' somewhere on your USB
drive. The root directory of the drive is probably the best place for
both folders to go to, although any number of intermediate directories
is acceptable. For the portable installation to work it is necessary
that the files which follow in this document be put to the same parent
directory as the `texmf' and `localtexmf' folders.
Now we are going to copy the original MiKTeX installation, which could
reside, say, in `C:\Program Files\MiKTeX 2.5', to the USB drive. Copy
everything you find in this folder (but not the folder itself!) into
the `texmf' directory on your USB drive. The `texmf' directory tree is
the one that will be maintained by the Package Manager and the Update
Wizzard. The `localtexmf' tree is the place where TeXperts will put
their own or third party packages.
Copy the `gs' folder (containing the `fonts' directory) of a Ghostscript
installation and the `Ghostgum' folder of a GSview installation to the
same parent directory which already contains the `texmf' and `localtexmf'
folders.
The next thing we need is the `reg.exe' utility, version 3.0, such that
we can manipulate the Windows Registry from within our scripts. It
resides in the system directory (c:\WINDOWS\system32) of Windows XP
or 2003 computers. However, there is no `reg.exe' on Windows 2000
computers. Make a copy of `reg.exe' to the parent directory of your
portable installation. It is a good idea to have it there in case you
want to plug your USB stick to a Windows 2000 computer.
3 Adding the scripts
====================
Now add all the DOS batch (.bat) scripts you find at the end of this
document to the same parent directory which contains the other folders
we have copied so far. With the exception of `versions.bat' none of
the scripts needs any modification.
`versions.bat', which is not intended to be run directly, is called by
some of the remaining scripts. It sets three script variables to the
version numbers of MiKTeX, Ghostscript and GSview. Edit this file and
make sure that the numbers therein match the actual program versions
of your installation. Line breaks must follow immediately after the
numbers. Spaces around them are not allowed.
########################################################################
*NB*: The `MIKTEX_VERSION', `GS_VERSION' and `GSVIEW_VERSION' variables
which are set in the file `versions.bat' need to be adjusted
according to the program versions being in use.
########################################################################
Don't forget to edit `versions.bat' whenever you update MiKTeX,
Ghostscript and/or GSview to a different version!
The purpose of each script is listed in the following.
versions.bat Called by `init.bat', `reset.bat' and `cmd.bat'. Sets the
`MIKTEX_VERSION', `GS_VERSION' and `GSVIEW_VERSION'
variables.
init.bat Initialises the MiKTeX, Ghostscript and GSview installations
on the USB drive. Associates the .dvi file name extension
with Yap and the .ps/.eps extensions with GSview. Backups
current user's file associations and application settings
of possibly existing MiKTeX and GSview installations on the
host computer and loads settings of the previous portable
session instead. Creates a context menu item for files and
folders, which allows to open a DOS command line prompt for
TeXifying documents at the location of a file or folder by
right click.
reset.bat To be run after finishing a session and before disconnecting
the USB or network drive. Restores previous file associations
and application settings. Saves the settings of the portable
session to the USB drive.
mpm.bat Starts the MiKTeX Package Manager for installation of
additional packages. The Update Wizzard can be started from
within the package manager via the `Task' menu.
mo.bat Starts the MiKTeX Options setting tool.
yap.bat Yet another means to open Yap. Drag and drop a .dvi file on
`yap.bat'.
gsview.bat Yet another means to open GSview. Drag and drop a Postscript
file on `gsview.bat'.
cmd.bat Yet another means to open a DOS command line window. Drag and
drop a file or directory on `cmd.bat' to open the command
line at the location of the file/directory.
The final directory structure should be as follows:
4 Using the portable installation
=================================
After connecting the USB drive to a Windows computer, the initialization
process is started by double click on `init.bat'. A DOS window opens,
displaying the output of the `initexmf' program run by the script.
Please wait until you are asked to hit a key for closing the window.
Once the DOS window is closed, the portable MiKTeX/Ghostscript/GSview
installation is ready for use. The most comfortable way to TeXify a
document is to open a DOS command line at its location: Right click
on a folder or file and select "Command Prompt Here" from the context
menu. Alternatively, you can drag and drop a folder or file onto
the `cmd.bat' icon. At the command line you will have access to all
executables (latex, pdflatex, bibtex, dvips, ps2pdf, ...) necessary to
process your documents. The resulting .dvi and .ps files can easily be
opened by double click, since appropriate file associations have been
established during initialization.
Before disconnecting the USB drive, it is a good idea to run `reset.bat'
in order to undo the changes which have been made to the Registry and to
restore personal application settings which were in effect before the
session. This ensures to wipe out any traces of your portable session
and to avoid possible interferences with existing MiKTeX/GS/GSview
installations on the host computer.
5 DOS batch scripts
===================
5.1 versions.bat
=================
--8<----------------------------------------------------------------------
rem MiKTeX
set MIKTEX_VERSION=2.5
rem Ghostscript
set GS_VERSION=8.56
rem GSview
set GSVIEW_VERSION=4.8
-->8----------------------------------------------------------------------
5.2 init.bat
============
--8<----------------------------------------------------------------------
@echo off
rem set path
set path=%~dp0texmf\miktex\bin;%path%
rem test whether a lock entry exists in the Registry
reg query HKCU\Software\TeX-portable 2>nul 1>nul && (
echo `%~nx0' can be run only once per session, run `reset.bat' first!
echo.
pause
exit
)
cls
echo Please wait...
rem set version numbers
call "%~dp0versions.bat"
rem add lock entry to the Registry
reg add HKCU\Software\TeX-portable /f 2>nul 1>nul
rem create folder context menu item for opening a DOS command line
reg add "HKCU\Software\Classes\Folder\shell\ZCmdHere" /ve /t REG_SZ /d "Command Prompt Here" /f 1>nul 2>nul
reg add "HKCU\Software\Classes\Folder\shell\ZCmdHere\command" /ve /t REG_SZ /d "\"%~dp0cmd.bat\" \"%%1\"" /f 1>nul 2>nul
rem create file context menu item for opening a DOS command line
reg add "HKCU\Software\Classes\*\shell\ZCmdHere" /ve /t REG_SZ /d "Command Prompt Here" /f 1>nul 2>nul
reg add "HKCU\Software\Classes\*\shell\ZCmdHere\command" /ve /t REG_SZ /d "\"%~dp0cmd.bat\" \"%%1\"" /f 1>nul 2>nul
rem backup existing .dvi entry
reg query HKCU\Software\Classes\.dvi 2>nul 1>nul && (
reg copy HKCU\Software\Classes\.dvi HKCU\Software\Classes\.dvi.bak /f /s 2>nul 1>nul
)
rem create new (private) file association for .dvi
reg add HKCU\Software\Classes\.dvi /ve /d "my_dvifile" /f 2>nul 1>nul
reg add HKCU\Software\Classes\my_dvifile\shell\open\command /ve /d "\"%~dp0texmf\miktex\bin\yap.exe\" \"%%1\"" /f 2>nul 1>nul
reg add HKCU\Software\Classes\my_dvifile\DefaultIcon /ve /d "\"%~dp0texmf\miktex\bin\yap.exe\"" /f 2>nul 1>nul
rem backup existing .ps/.eps entries
reg query HKCU\Software\Classes\.ps 1>nul 2>nul && (
reg copy HKCU\Software\Classes\.ps HKCU\Software\Classes\.ps.bak /s /f 2>nul 1>nul
)
reg query HKCU\Software\Classes\.eps 1>nul 2>nul && (
reg copy HKCU\Software\Classes\.eps HKCU\Software\Classes\.eps.bak /s /f 2>nul 1>nul
)
rem create new (private) file associations for .ps/.eps
reg add HKCU\Software\Classes\.ps /ve /d "my_psfile" /f 2>nul 1>nul
reg add HKCU\Software\Classes\.eps /ve /d "my_psfile" /f 2>nul 1>nul
reg add HKCU\Software\Classes\my_psfile\shell\open\command /ve /d "\"%~dp0Ghostgum\gsview\gsview32.exe\" \"%%1\"" /f 2>nul 1>nul
reg add HKCU\Software\Classes\my_psfile\DefaultIcon /ve /d "\"%~dp0Ghostgum\gsview\gsview32.ico\"" /f 2>nul 1>nul
rem backup existing gsview32.ini file in the %USERPROFILE% directory
if exist "%USERPROFILE%\gsview32.ini" (
del "%USERPROFILE%\gsview32.ini.bak" 1>nul 2>nul
ren "%USERPROFILE%\gsview32.ini" gsview32.ini.bak 1>nul 2>nul
)
rem write gsview32.ini file on USB drive
echo [GSview-%GSVIEW_VERSION%]> "%~dp0Ghostgum\gsview\gsview32.ini"
echo Configured=1 >> "%~dp0Ghostgum\gsview\gsview32.ini"
echo Version=%GSVIEW_VERSION%>> "%~dp0Ghostgum\gsview\gsview32.ini"
echo GhostscriptDLL=%~dp0gs\gs%GS_VERSION%\bin\gsdll32.dll>> "%~dp0Ghostgum\gsview\gsview32.ini"
echo GhostscriptEXE=%~dp0gs\gs%GS_VERSION%\bin\gswin32c.exe>> "%~dp0Ghostgum\gsview\gsview32.ini"
echo GhostscriptInclude=%~dp0gs\gs%GS_VERSION%\lib;%~dp0gs\fonts;%~dp0gs\gs%GS_VERSION%\Resource>> "%~dp0Ghostgum\gsview\gsview32.ini"
echo GhostscriptOther=-dNOPLATFONTS -sFONTPATH="c:\psfonts">> "%~dp0Ghostgum\gsview\gsview32.ini"
echo. >> "%~dp0Ghostgum\gsview\gsview32.ini"
rem append GSview settings from last session
if exist "%~dp0.gsview32.ini" (
type "%~dp0.gsview32.ini" >> "%~dp0Ghostgum\gsview\gsview32.ini"
)
rem backup existing MiKTeX settings for later restore
reg query HKCU\Software\MiKTeX.org\MiKTeX\%MIKTEX_VERSION% 1>nul 2>nul && (
reg copy HKCU\Software\MiKTeX.org\MiKTeX\%MIKTEX_VERSION% HKCU\Software\MiKTeX.org\MiKTeX\%MIKTEX_VERSION%.bak /s /f 1>nul 2>nul
reg delete HKCU\Software\MiKTeX.org\MiKTeX\%MIKTEX_VERSION% /f 1>nul 2>nul
)
reg query HKLM\Software\MiKTeX.org\MiKTeX\%MIKTEX_VERSION% 1>nul 2>nul && (
reg copy HKLM\Software\MiKTeX.org\MiKTeX\%MIKTEX_VERSION% HKLM\Software\MiKTeX.org\MiKTeX\%MIKTEX_VERSION%.bak /s /f 1>nul 2>nul
reg delete HKLM\Software\MiKTeX.org\MiKTeX\%MIKTEX_VERSION% /f 1>nul 2>nul
)
rem load MiKTeX settings from previous session
if exist "%~dp0.MiKTeX.reg" (
reg import "%~dp0.MiKTeX.reg" 1>nul 2>nul
)
rem on startup, don't let Yap check whether it is the default viewer (this breaks the .dvi file association)
reg add HKCU\Software\MiKTeX.org\MiKTeX\%MIKTEX_VERSION%\Yap\Settings /v "Check Associations" /d 0 /t REG_DWORD /f 1>nul 2>nul
rem initialize MiKTeX
reg add HKCU\Software\MiKTeX.org\MiKTeX\%MIKTEX_VERSION%\Core /v "Install" /d "%~dp0texmf" /t REG_SZ /f 1>nul 2>nul
reg add HKCU\Software\MiKTeX.org\MiKTeX\%MIKTEX_VERSION%\Core /v "Roots" /d "%~dp0localtexmf;%~dp0texmf" /t REG_SZ /f 1>nul 2>nul
reg add HKCU\Software\MiKTeX.org\MiKTeX\%MIKTEX_VERSION%\Core /v "UserData" /d "%~dp0.userdata" /t REG_SZ /f 1>nul 2>nul
reg add HKCU\Software\MiKTeX.org\MiKTeX\%MIKTEX_VERSION%\Core /v "UserConfig" /d "%~dp0.userconfig" /t REG_SZ /f 1>nul 2>nul
initexmf.exe --verbose --dump --mklinks
initexmf.exe --verbose --mkmaps
initexmf.exe --verbose --update-fndb
cls
echo.
echo The portable MiKTeX/GS/GSview installation is now ready for use!
echo.
echo *********************************************************************
echo ** IMPORTANT: Don't forget to run `reset.bat' before disconnecting **
echo ** the USB drive! **
echo *********************************************************************
echo.
pause
-->8----------------------------------------------------------------------
5.3 reset.bat
=============
--8<----------------------------------------------------------------------
@echo off
set path=%~dp0Ghostgum\gsview;%path%
rem test whether a lock entry exists in the Registry
reg query HKCU\Software\TeX-portable 2>nul 1>nul || (
echo There is nothing to be reset. Aborting...
echo.
pause
exit
)
echo.
echo **********************************************************************
echo ** IMPORTANT: Make sure that all instances of Yap have been closed! **
echo ** GSview will close automatically. **
echo **********************************************************************
echo.
pause
rem close all instances of GSview
gsview32 -x
cls
echo Please wait...
rem set version numbers
call "%~dp0versions.bat"
rem restore previous file associations
reg delete HKCU\Software\Classes\.dvi /f 2>nul 1>nul
reg query HKCU\Software\Classes\.dvi.bak 1>nul 2>nul && (
reg copy HKCU\Software\Classes\.dvi.bak HKCU\Software\Classes\.dvi /s /f 2>nul 1>nul
reg delete HKCU\Software\Classes\.dvi.bak /f 2>nul 1>nul
)
reg delete HKCU\Software\Classes\.ps /f 2>nul 1>nul
reg query HKCU\Software\Classes\.ps.bak 1>nul 2>nul && (
reg copy HKCU\Software\Classes\.ps.bak HKCU\Software\Classes\.ps /s /f 2>nul 1>nul
reg delete HKCU\Software\Classes\.ps.bak /f 2>nul 1>nul
)
reg delete HKCU\Software\Classes\.eps /f 2>nul 1>nul
reg query HKCU\Software\Classes\.eps.bak 1>nul 2>nul && (
reg copy HKCU\Software\Classes\.eps.bak HKCU\Software\Classes\.eps /s /f 2>nul 1>nul
reg delete HKCU\Software\Classes\.eps.bak /f 2>nul 1>nul
)
reg delete HKCU\Software\Classes\my_dvifile /f 2>nul 1>nul
reg delete HKCU\Software\Classes\my_psfile /f 2>nul 1>nul
rem dump current MiKTeX settings to `.MiKTeX.reg' on the USB drive
reg query HKCU\Software\MiKTeX.org\MiKTeX\%MIKTEX_VERSION% 1>nul 2>nul && (
reg export HKCU\Software\MiKTeX.org\MiKTeX\%MIKTEX_VERSION% "%~dp0.MiKTeX.reg" 1>nul 2>nul
)
rem restore previous MiKTeX settings
reg delete HKCU\Software\MiKTeX.org\MiKTeX\%MIKTEX_VERSION% /f 1>nul 2>nul
reg query HKCU\Software\MiKTeX.org\MiKTeX\%MIKTEX_VERSION%.bak 1>nul 2>nul && (
reg copy HKCU\Software\MiKTeX.org\MiKTeX\%MIKTEX_VERSION%.bak HKCU\Software\MiKTeX.org\MiKTeX\%MIKTEX_VERSION% /s /f 1>nul 2>nul
reg delete HKCU\Software\MiKTeX.org\MiKTeX\%MIKTEX_VERSION%.bak /f 1>nul 2>nul
)
reg query HKLM\Software\MiKTeX.org\MiKTeX\%MIKTEX_VERSION%.bak 1>nul 2>nul && (
reg query HKLM\Software\MiKTeX.org\MiKTeX\%MIKTEX_VERSION% 1>nul 2>nul && (
reg delete HKLM\Software\MiKTeX.org\MiKTeX\%MIKTEX_VERSION% /f 1>nul 2>nul
)
reg copy HKLM\Software\MiKTeX.org\MiKTeX\%MIKTEX_VERSION%.bak HKLM\Software\MiKTeX.org\MiKTeX\%MIKTEX_VERSION% /s /f 1>nul 2>nul
reg delete HKLM\Software\MiKTeX.org\MiKTeX\%MIKTEX_VERSION%.bak /f 1>nul 2>nul
)
rem copy current GSview settings to USB drive
if exist "%USERPROFILE%\gsview32.ini" (
copy "%USERPROFILE%\gsview32.ini" "%~dp0.gsview32.ini" 1>nul 2>nul
del "%USERPROFILE%\gsview32.ini" 1>nul 2>nul
)
rem restore backup version of gswin32.ini
if exist "%USERPROFILE%\gsview32.ini.bak" (
ren "%USERPROFILE%\gsview32.ini.bak" gsview32.ini
)
rem delete folder and file context menu item
reg delete HKCU\Software\Classes\Folder\shell\ZCmdHere /f 1>nul 2>nul
reg delete HKCU\Software\Classes\*\shell\ZCmdHere /f 1>nul 2>nul
rem delete lock entry in the Registry
reg delete HKCU\Software\TeX-portable /f 2>nul 1>nul
cls
echo You can now disconnect the USB drive!
echo.
pause
-->8----------------------------------------------------------------------
5.4 mpm.bat
===========
--8<----------------------------------------------------------------------
@echo off
set path=%~dp0texmf\miktex\bin;%path%
start mpm.exe
-->8----------------------------------------------------------------------
5.5 mo.bat
===========
--8<----------------------------------------------------------------------
@echo off
set path=%~dp0texmf\miktex\bin;%path%
start mo.exe
-->8----------------------------------------------------------------------
5.6 yap.bat
===========
--8<----------------------------------------------------------------------
@echo off
set path=%~dp0texmf\miktex\bin;%path%
start yap.exe %*
-->8----------------------------------------------------------------------
5.7 gsview.bat
==============
--8<----------------------------------------------------------------------
@echo off
set path=%~dp0Ghostgum\gsview;%path%
start gsview32.exe %*
-->8----------------------------------------------------------------------
5.8 cmd.bat
===========
--8<----------------------------------------------------------------------
@echo off
rem set version numbers
call "%~dp0versions.bat"
rem set path to executables
set path=%~dp0gs\gs%GS_VERSION%\lib;%~dp0gs\gs%GS_VERSION%\bin;%~dp0texmf\miktex\bin;%path%
rem MiKTeX environment
if not exist texmf\nul (
rem extra paths to locate .bib files.
set BIBINPUTS=.//
rem Extra paths to locate .bst files.
set BSTINPUTS=.//
rem Extra paths to locate METAFONT input and openin files.
set MFINPUTS=.//
rem Extra paths to locate MetaPost input files.
set MPINPUTS=.//
rem Extra paths to locate TeX \input and \openin files.
set TEXINPUTS=.//
)
rem Ghostscript environment
set GS_DLL=%~dp0gs\gs%GS_VERSION%\bin\gsdll32.dll
set GS_LIB=%~dp0gs\gs%GS_VERSION%\lib;%~dp0gs\fonts;%~dp0gs\gs%GS_VERSION%\Resource
rem chdir to the first argument or, if it's a file, to its parent dir
(
%~d1
cd %1 || cd "%~p1"
) 1>nul 2>nul
rem open console
cmd.exe
-->8----------------------------------------------------------------------