How do I show programming keywords differently?

How do I show programming keywords differently?

Slack the software program, like this website has a very neat feature. When I type backticks like so: `` I am able to write any programmer word in it. Like: someFunction().

How do I do this in LaTeX? I want a similar effect in the output pdf.

I am pretty sure the question is already answered, but I can't think of the right search terms to find it. The scavenger hunt for search terms is real.

답변1

Here's a LuaLaTeX-based solution. TeX-special characters, such as _ and &, may occur between pairs of backtick characters. Addendum to incorporate a follow-up comment by the OP: Care needs to be taken in order not to mis-interpret consecutive backtick characters -- those are generally used in TeX and LaTeX documents to initiate some instance of quoted material!

enter image description here

% !TEX TS-program = lualatex
\documentclass{article}
\usepackage{luacode}
\begin{luacode}
function backticks2tt ( s )
  return ( s:gsub ( "`(..-)`" , "\\texttt{\\detokenize{%1}}" ) )
end
\end{luacode}
\AtBeginDocument{\directlua{luatexbase.add_to_callback(
  "process_input_buffer", backticks2tt, "backticks2tt" )}}
\usepackage{url}

\begin{document}
aa `someFunction()` bb `some_other_Function()` cc
\end{document} 

A comment on the type of pattern matching that's performed by the Lua function backticks2tt seems indicated. The pattern

`(..-)`

is the "non-greedy" way of specifying the pattern "all instances of one or more characters that are encased by backtick characters". To specify a "greedy" pattern match of "one or more characters" in Lua, one would type

`.+`

However, a greedy pattern match wouldn't be appropriate here, as it would end up grabbing the entire string

someFunction()` bb `some_other_Function()

in the MWE shown above and changing it to

\texttt{\detokenize someFunction()` bb `some_other_Function()}

Clearly, that's not right, and that's why it's necessary to specify non-greedy pattern matching.

답변2

Based on Mico's answer I got a bit more serious. For some reason, I still experienced some issues. So I decided to learn some Lua through some articles and testing it on Repl.it. The website allows me to test and print things quickly.

On Repl.it I iterated on what I wanted until I came to my final design and tested the following code:

function replaceBacktick(input)
  local backtickPattern = "`(..-)`"
  local quotePattern = "`(..-)'"
  isQuoteMatch = input:find(quotePattern)
  if isQuoteMatch == nil then
    print("this is not a quote")
    print( input:gsub(backtickPattern, "\\texttt{" .. input .. "}") )
  else
    print("this is a quote")
  end
end


replaceBacktick( "``the number of correctly identified instances divided by the total number of instances \cite{leong2016} '') and 2.53\% (sensitivity which``")
replaceBacktick("`SomeReactComponent`")

I started working from Mico his answer, because working from my tested Repl.it code resulted in errors and also was a lot more verbose. Still, it helped since I learned how to write if-statements and functions, I didn't know the syntax before.

I made a huge assumption that not triggering the return statement in the if-statement would be fine and would leave text in LaTeX as is (it apparently is). Printing for debugging purposes in LaTeX, even after reading some articles, is a mystery to me.

Anyways, this is the final code that works for me and ignores quotes, even single quotes!

\usepackage{luacode}
\begin{luacode}
function backticks2tt ( input )
    local isQuoteMatch = input:find("`(..-)'")
    if isQuoteMatch == nil then
        return ( input:gsub ( "`(..-)`" , "\\inlinecode{\\detokenize{%1}}" ) )
    end
end
\end{luacode}
\AtBeginDocument{\directlua{luatexbase.add_to_callback(
  "process_input_buffer", backticks2tt, "backticks2tt" )}}

관련 정보