3

This answer shows how one line inside of lstlisting can be set in bold face. However, in this solution, you need to put the material in a lstlisting environment.

But what if I need to input the line using \lstinputlisting? What should I do in this case?

Editing the input file is not an option. This file is machine generated and is likely to change from time-to-time. But the line number of the line to be bolded does not change.

Though we don't need an MWE, I am providing one so that you can provide a solution with minimum hassle.


\documentclass{article}

\usepackage{filecontents}

\begin{filecontents*}{\jobname.txt}
First
Second
Third line to be bolded.
Fourth
\end{filecontents*}

\usepackage{listings}

\begin{document}

\lstinputlisting{\jobname.txt}

\end{document}

2 Answers 2

3

listings has a lot of hooks you can (ab)use, here for example OutputBox, which is called at the beginning of each line (after possibly typesetting a line number).

Additions to hooks are global, so if you want to limit the scope, use a macro and redefine it as required:

\documentclass{article}
\usepackage{listings}

\makeatletter
\lst@AddToHook{OutputBox}{\lst@boldline}
\let\lst@boldline\relax
\newcommand\lstboldline[1][3]{% optional argument to choose the line number?
    \def\lst@boldline{\ifnum\lst@lineno=#1 \bfseries\fi}}
\newcommand\lstnoboldline{\let\lst@boldline\relax}
\makeatother

\begin{document}
\lstboldline
\lstinputlisting{\jobname.txt}

\lstnoboldline
\lstinputlisting{\jobname.txt}

\lstboldline[2]
\lstinputlisting{\jobname.txt}
\end{document}

output

3
  • Looks like the hook becomes global even when set inside a scope. How do I unset this?
    – Masroor
    Aug 14, 2017 at 14:13
  • @Masroor See edit...
    – Robert
    Aug 14, 2017 at 21:43
  • Is there any way to extend this to support multiple bold lines? i.e. \lstboldline[2,4] or similar?
    – Flimzy
    Jul 8, 2020 at 11:02
2

My approach:

  • You can read the file line for line using this answer to Read arbitrary lines from file
  • use a forloop and a if condition to adapt the specified line to make it bold
  • write each line using \immediate\write\tempfile{...}
  • use \lstinputlisting[escapeinside={(*}{*)}]{\jobname.tmp} to show the adapted code

For this I made the command boldlineCode{<number of lines>}{<bold line>}.

\documentclass{article}

\usepackage{filecontents}

\begin{filecontents*}{\jobname.txt}
First
Second
Third line to be bolded.
Fourth
\end{filecontents*}

\usepackage{listings}
\usepackage{forloop}
\usepackage{ifthen}

\newcounter{ct}

% boldlineCode
% #1 = number of lines
% #2 = bold line
\newcommand{\boldlineCode}[2]{
    \newwrite\tempfile
    \immediate\openout\tempfile=\jobname.tmp
    \newread\myread
    \openin\myread=\jobname.txt
    % read lines, insert bold command in line #2 and write
    \forloop{ct}{1}{\numexpr\value{ct}-1 < #1}%
    {%
        \read\myread to \command
        \ifthenelse{\value{ct}=#2}{
            \immediate\write\tempfile{(*\unexpanded{\bfseries} \command*)}
        }{
            \immediate\write\tempfile{\command}
        }
    }
    \immediate\closeout\tempfile
    \lstinputlisting[escapeinside={(*}{*)}]{\jobname.tmp}
}

\begin{document}

\boldlineCode{4}{3}

\boldlineCode{4}{2}

\end{document}

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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