# How do I reference the main .tex file when writing user-defined commands in TeXworks?

I am trying to create user-defined commands in TeXworks, but am having problems finding the syntax to reference the main .tex file. For instance, creating a .bat-file and then a new "typesetting tool" in TeXworks pointing to that .bat file works fine if the contents is the following line:

``````pdflatex "mylatexfile.tex"
``````

"mylatexfile.tex" then compiles without problems, and the first line of the compiler output is

``````C:\mypath>pdflatex "mylatexfile.tex"
``````

The following, however, doesn't work:

``````pdflatex "%1.tex"
``````

The compiler then starts with the line

``````C:\mypath>pdflatex ".tex"
``````

and ends with "...File ignored)...". Am I wrong to assume that "%1" references the main latex file? (That's what I understood from http://code.google.com/p/texworks/wiki/AdvancedTypesettingTools#tex_-%3E_dvips_-%3E_ps2pdf.)

In the latex editor LEd, I've successfully created user-defined commands using .bat files. There, however "%2" is supposed to reference the main latex file. I've tried that in TeXworks but to no avail. I get the same result as using "%1".

Any tips?

I'm using MiKTeX 2.9 and Windows 7.

-
The `%n` variable expands to the `(n+1)`th parameter passed in the command line. If you call `test.bat a b c`, the pairs will be `%0=test.bat`, `%1=a`, `%2=b` and `%3=c`. My guess is that you are calling your `bat` file without passing the filename, so `%1=`. Try to call it providing the filename, say, `test.bat myfile`, so `%1.tex` will be expanded to `myfile.tex`. –  Paulo Cereda Dec 8 '11 at 11:00
Ah! Adding the argument "\$fullname" in the dialog box for configuring my user-defined "typesetting tool" in TeXworks enabled me to reference the file name using "%1" in the .bat file. Thanks a lot! –  andreasdr Dec 8 '11 at 12:41

The use of `%1`, `%2` and so on makes sense within a batch file, and picks up the arguments it is supplied at the command line. So if I had a minimal batch file `demo.bat`:

```pdflatex "%1.tex"
```

I would use it at the Command Line as

```demo mylatexfile
```

What I think you are doing is trying to use `%1` to refer to the main file when executing the batch file from TeXworks. However, TeXworks uses the code `\$fullname` to refer to the main TeX file including the extension, and `\$basename` for the name minus the extension. For example, the set up I have for running pdfLaTeX directly is:

So to run my example batch file, which needs one argument, I would have something like

where I'd need one line in the 'Arguments' box for each one the batch file needs (`%1`, `%2`, and so on).

-
Great, thanks for that! Now I finally understand the process: Running the typesetting tool "demo" (as you defined it above) from within TeXworks with "mylatexfile.tex" open is equivalent to typing, in a command window: "demo.bat mylatexfile". This means that demo.bat, which contains "pdflatex "%1.tex"", will replace %1 with mylatexfile and thus execute the command "pdflatex "mylatexfile.tex"". There, I just wanted to write that down for myself :). –  andreasdr Dec 8 '11 at 14:18