1

I am using lstinputlisting to reference a piece of source code, for which has a caption and label, as shown below.

\lstinputlisting[caption={Caption}, label={code:arbitrary}]{directory/arbitrary}

My desire is to highlight specific lines of the source code, I tried researching this and found people with similar issues, and the suggestion is to use \usepackage{lstlinebgrd}.

However, whenever I implement this package into my tex file I get the following error appear at my,

\documentclass[11pt]{report}

File `fancyhdr.sty' not found.

EDIT: It appears that wherever I the \usepackage{lstlinebgrd} the next line will throw an error, for example if I were to do

\usepackage{lstlinebgrd}

\usepackage{apacite}

I get the error File 'apacite.sty' not found.

EDIT 2: As requested a minimum working example is below.

\documentclass[11pt]{report}
\usepackage{lstlinebgrd}
\usepackage{apacite}
\usepackage{listings}

\begin{document}
\lstinputlisting[caption={test}, label={test}]{test}
\end{document}
9
  • Neither lstlinebgrd.sty nor listings.sty refer to fancyhdr, so I guess the error must come from something else. Moreover, fancyhdr.sty not found is very strange, as it should be a standard package in your TeX system. Please check your log file to see which package or line causes the error message. Commented Mar 4, 2021 at 15:03
  • @PietervanOostrum I have edited the question to elaborate further. Commented Mar 4, 2021 at 16:09
  • Are you using a complicated style with your listing? Can you provide a complete, minimal example that replicates your setup? Something that starts with \documentclass and ends with \end{document} including sample code you want to include? Can you do that?
    – Werner
    Commented Mar 4, 2021 at 16:45
  • @Werner edited question based on your request. Commented Mar 4, 2021 at 20:53
  • @user41245213: So you don't use any special code formatting? For example, what is in test? Is it Python code, or perhaps C++, or just something you want printed verbatim?
    – Werner
    Commented Mar 4, 2021 at 21:05

1 Answer 1

0

Here's an option: You manually specify the lines you want coloured (and with what <colour>) using \lstcolourline{<colour>} and using the escapechar=<char> to insert it in the appropriate lines in your code.

enter image description here

\documentclass{article}

\usepackage{xcolor}
\usepackage{listings}

\newcommand{\lstcolourline}[1]{\rlap{\color{#1}\rule[-.3\baselineskip]{\linewidth}{\baselineskip}}}

\begin{filecontents*}[overwrite]{test.txt}
first line
second line
@\lstcolourline{green!15}@third line
fourth line
@\lstcolourline{red!15}@fifth line
sixth line
\end{filecontents*}

\begin{document}

\lstinputlisting[caption={test},escapechar=@]{test.txt}

\end{document}
2
  • The issue with this is that it requires the code to be pasted into the tex file. Instead I want to purely use lstinputlisting to grab the source code for me. Unless I am mistaking the functionality of your tex file. Commented Mar 4, 2021 at 22:03
  • @user41245213: You'll have to manually insert the @\lstcolourline{<colour>}@ entries within your file (test.txt). I created test.txt as part of my minimal code using filecontents.
    – Werner
    Commented Mar 4, 2021 at 22:06

You must log in to answer this question.

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