# How insert a character at the begin of every line from a source code?

I'm writing a lecture with many source codes for example. The codes are stored in severus different files. I use listing and minted packages for formatting the sources.

I would like add character at the begin of every line of some codes like:

• Bash shell ($or #) • Python shell (>>>) • Matlab (>>) • Other shells (>) I tried some solutions which have given on: But both solutions have the same problem when I try: \begin{foo_environment} \input{bar.txt} \end{foo_environment}  In both solutions the linebreaking are ingnored I have tried executing a bash command: sed 's/^/$ /' bar.sh > bar.txt


This works locally but I can't do the same in Overleaf and Sharelatex

This is a sample code bar.sh:

cd ..
mount /dev/sda3 /mnt/
cd ~/.ssh/
ll
cd ~
ls -al


The result must be:

$cd ..$ mount /dev/sda3 /mnt/
$cd ~/.ssh/$ ll
$cd ~$ ls -al

• Could you make this a complete example, by adding everything from \documentclass through \end{document}? That gives us something to start from. Nov 21, 2016 at 13:00
• Ofcourse. I'll adding in some minutes. Thanks Nov 21, 2016 at 13:12

With listings you can redefine the numberstyle:

\documentclass{article}
\usepackage{listings}

\lstset{
language=tex,
basicstyle=\footnotesize\ttfamily\selectfont,
keepspaces=true,
numbers=left,
numbersep=5pt,
numberstyle=\numberwithprompt,
}

\newcommand{\lstprompt}{>>>}
\newcommand\numberwithprompt[1]{\footnotesize\ttfamily\selectfont#1 \lstprompt}

\begin{document}
\begin{lstlisting}
a
b
c
\end{lstlisting}
\end{document}


minted.sty uses fancyvrb.sty to typeset the minted environments. fancyvrb.sty provides a macro named \FancyVerbFormatLine to change individual line formatting. You can define your own macro for your environment and plug it into minted with the formatcom key. Code:

\documentclass{article}

\usepackage{minted}

\newcommand{\BashFancyFormatLine}{%
\def\FancyVerbFormatLine##1{\$\,##1}% } \begin{document} \noindent Some Text \begin{minted}[formatcom=\BashFancyFormatLine]{bash} cd .. mount /dev/sda3 /mnt/ cd ~/.ssh/ ll cd ~ ls -al \end{minted} some text \end{document}  You can apply the same procedure to external files by using \inputminted: \documentclass{article} \usepackage{minted,filecontents} \begin{filecontents*}{bash.sh} cd .. mount /dev/sda3 /mnt/ cd ~/.ssh/ ll cd ~ ls -al \end{filecontents*} \newcommand{\BashFancyFormatLine}{% \def\FancyVerbFormatLine##1{\$\,##1}%
}

\begin{document}

\noindent Now read the same code from a file:
\inputminted[formatcom=\BashFancyFormatLine]{bash}{bash.sh}

\end{document}

• Thank you. That is a more elegant way to solve. But the problem is when We use \input{bar.txt} for import a text file insted to use a text into the .tex file Nov 21, 2016 at 14:15
• @AdolfoCorrea - See update in my answer. Nov 21, 2016 at 14:31

With listings you can hook into the line numbering code. This approach is fine if you don't put \labels in your code -- I never have and didn't even know you could -- but if you do, when you refer to the line numbers they will be followed by whatever prompt text you define. In that case see Ulrike Fischer's answer.

Here's an example, which defines a "prompt" (\lstprompt) as ">>>" (python style) in line 13 and applies it in line 14.

\documentclass{article}
\usepackage{listings}

\lstset{
language=tex,
basicstyle=\footnotesize\ttfamily\selectfont,
keepspaces=true,
numbers=left,
numbersep=5pt,
numberstyle=\footnotesize\ttfamily\selectfont\,
}

\newcommand{\lstprompt}{>>>}
\renewcommand*\thelstnumber{{\the\value{lstnumber}}\lstprompt}
\begin{document}
\lstinputlisting{lst_test.tex}
\end{document}


And the output (using the code above as a test case):

Of course you can insert spaces or otherwise tweak at will. Here's a $sign with no numbering: \newcommand{\lstprompt}{\$}
\renewcommand*\thelstnumber{\lstprompt}


• Thank you for your help. I'm going to test your solution and next I'm goin to vote. Nov 21, 2016 at 14:18
• I wouldn't redefine \thelstnumber -- it can also be used for references and the additional code would disturb this. Nov 21, 2016 at 14:34
• @UlrikeFischer having used listings without referring to line numbers -- in fact only to the (sub)section containing the listing -- it would seem reasonable in some cases to do this. I also considered whether the line number separator might provide a way in (which in many ways I would prefer. Nov 21, 2016 at 15:16
• @UlrikeFischer now I know that putting labels inside your listings is even possible, I've added a note to the top of my answer commending yours for those who use such things. Nov 21, 2016 at 15:57