2

The goal: I'm trying to make a project. I have a program which is compiled and executed by batch script (Windows Batch File). There are several of those.

The problem: I would like to have them listed on my overleaf/latex document but cannot find proper language. On the top of that citing seems to be cutting off some letters (like 'c' is missing') and somehow LaTeX says there are 4 rows instead of 3.

LaTeX config:

   \usepackage[utf8]{inputenc}
   \usepackage[T1]{fontenc}
   %----fonts
   \usepackage{roboto}
   \usepackage{mathptmx}
   \usepackage{charter}
   \usepackage{inconsolata}

What I'm trying to cite

\begin{lstlisting}[caption={Trimming silence script},label={lst:scr1},language=command.com]

if not exist "C:\Users\Folder1\Script1" mkdir C:\Users\Folder1\Script1
for /l %x in (1, 1, 100) do echo %%x
pause
\end{lstlisting}

Compiled file result:

code problem

I've read here that command.com might be suitable language but whatever change I make the colors etc. remains the same.

Including AutoHotkey and Batch file code in LaTeX with \usepackage{listings}

4
  • Welcome to TeX.SE! Please make your codesnippet be compilable, then we do not have to guess what you are doing ...
    – Mensch
    Commented Jun 29, 2022 at 11:33
  • @Mensch how can I do that?
    – janki3l
    Commented Jun 29, 2022 at 11:44
  • Make a full example, including document class, preamble, \begin/end{document}.
    – daleif
    Commented Jun 29, 2022 at 12:20
  • 1
    Also it might be better if you changed the title of your question, you're not really adding batch code to an overleaf project (I my book that means you have issues either uploading it to OL or running it in OL). Your issue is typesetting batch files via listings. Your title should reflect that.
    – daleif
    Commented Jun 29, 2022 at 12:21

2 Answers 2

1

You will need to define your own language, since batch is not defined in the listings package.

Anyway, after a google search I could find a definition of the PowerShell language (should be similar to batch) in the following github repository: Here

Use this snippet and add it before \begin{document}, then define language=PowerShell for your listing. You might need to adapt some keywords in the language definition to suit your needs.

Additionally, make sure that you don't use special characters in the code snippet. See here Also, your first line in the code snippet is empty, that's why there are four lines.

1

This answer incorporates some of the github file of ptrck's answer, but completes the rest of the tex file. A large part of the github file is taken up by morekeywords. In this case, I kept the control words (Do, Else, etc), and then added the five other words that appeared in your code sample. This means that this language definition is not as exhaustive as the one on github. If you're going to use this for several batch files, you'll need to make sure to enter all of the keywords that you use. (On the other hand, the one on github is missing not, exist, and echo, and has mkdir twice.) I also made some guesses to have my output more closely match yours.

\documentclass{article}
\usepackage{xcolor}
\usepackage{listings}
\lstdefinelanguage{batch}{
    morekeywords={not,exist,mkdir,echo,pause},
    morekeywords={Do,Else,For,ForEach,Function,If,In,Until,While},
    alsodigit={-},
    sensitive=false,
    morecomment=[l]{\#},
    morecomment=[n]{<\#}{\#>},
    morestring=[b]{"},
    morestring=[b]{'},
    morestring=[s]{@'}{'@},
    morestring=[s]{@"}{"@},
    numbers=right,
    numberstyle=\tiny,
    basicstyle=\footnotesize,
    stringstyle=\color{green},
    keywordstyle=\color{blue},
    frame=tb,
}
\begin{document}
\begin{lstlisting}[caption={Trimming silence script},label={lst:scr1},language=batch]
if not exist "C:\Users\Folder1\Script1" mkdir C:\Users\Folder1\Script1
for /l %x in (1, 1, 100) do echo %%x
pause
\end{lstlisting}
\end{document}

output

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .