2

(This is my first question, I hope I'm not breaking any rules in how I'm asking it)
I would need an environment that basically takes as input a list of words separated by the ":" symbol and that only bolds what appears before the ":"

Practical example:

If I write in my code

\begin{environment}

pizza: italian food made of (etc...)
dog: animal that is similar to a wolf and (etc...)
John: name of persone that (etc...)
plastic: type of material given by (etc...)

\end{environment}

I would like my output to be:

pizza: italian food made of (etc...)
dog: animal that is similar to a wolf and (etc...)
John: name of persone that (etc...)
plastic: type of material given by (etc...)


In general other ":"s could also appear in the description of the words, so one thing to pay attention to is the fact that it is not condoned (Basically it must be in bold only before the first ":")

I had initially used chatGPT to try to create this environment but despite the various corrections I made to it (it generated a lot of wrong or non-working codes)

Motivation

I already have the list of words defined (there are about 4000 words), I would like to avoid going in front of each word and copying and pasting "\mathbf{}" or any other command like "\item" or similar. I would greatly prefer it if it were therefore an environment that only requires adding code to the beginning and end of the list.

4
  • 3
    it's possible but I would use \item it's only a minute editing and will make more robust code, but assuming you don't want that, what marks the start of each entry, is each entry on a single line in the source? Commented Oct 25, 2023 at 22:14
  • @DavidCarlisle Yes, think of it as if it were a dictionary, every definition is on the same line of code Commented Oct 25, 2023 at 22:24
  • I modified your example text a bit on the recommendation of @egreg Commented Oct 25, 2023 at 22:26
  • Although you now have a LaTeX solution, a good editor should be able to make lots of similar substitutions relatively easy. Being able to do regular expression substitutions like "replace everything up to the first colon with this thing in \textbf on every line" is a useful capability in my eyes (although regex are not that easy to master).
    – Dubu
    Commented Oct 30, 2023 at 11:15

3 Answers 3

7

enter image description here

I would probably edit the source rather than do this but...

\documentclass{article}

\newenvironment{environment}
{\obeylines
\everypar{\boldify}%
}
{}
\def\boldify#1:{\textbf{#1: }}
\begin{document}

\begin{environment}

pizza: italian food made of pineapples (etc...)
dog: animal that is similar to a wolf and (etc...)
John: name of persone that (etc...)
plastic: type of material given by (etc...)

\end{environment}


aaa: oops

\end{document}

7
  • The code works but continues to work outside of the environment. Is there a way to isolate it? (I tried with clips but it doesn't work) Commented Oct 25, 2023 at 22:48
  • Actually now I tried it again and if I write something outside the environment it gives me an error, it says: "File ended while scanning use of \boldify." Commented Oct 25, 2023 at 22:53
  • @MathAttack try now Commented Oct 25, 2023 at 22:56
  • It works now, very kind Commented Oct 25, 2023 at 23:00
  • 1
    @Mico no actually it will error in that case, you could do a 2 stage parse, first grab whole line then check for a : but I wouldn't do that. if the input isnt guaranteed safe I would edit the source just adding explicit markup where needed, and adjusting for any special lines Commented Oct 26, 2023 at 7:18
4

What we can do in OpTeX:

\def\begbf{\bgroup \everypar={\bftocolon}}
\def\endbf{\egroup}
\eoldef\bftocolon#1{\bftocolonA#1\par}
\def\bftocolonA#1:{{\bf#1:}}

\begbf

pizza: italian food made of pineapples (etc...)
dog: animal that is similar to a wolf and (etc...)
John: name of persone that (etc...)
plastic: type of material given by (etc...)

\endbf

\bye
2

Just for the sake of variety, a LuaLaTeX-based solution. It works by (a) creating a Lua function that employs Lua's string.gsub function to embolden the material up to but not including the first ":" character in the input string and (b) setting up a LaTeX environment called boldify, within which the Lua function is assigned to LuaTeX's process_input_buffer callback. There, the Lua function acts as a preprocessor on the input stream, before TeX starts its usual processing.

enter image description here

% !TEX TS-program = lualatex
\documentclass{article}

\usepackage{luacode} % for 'luacode' env.
\begin{luacode}
   function boldify ( s )
      return ( s:gsub ( "^(.-):" , "\\textbf{%1}:" ) )
   end
\end{luacode}

\newenvironment{boldify}{%
   \obeylines
   \directlua{luatexbase.add_to_callback (
      "process_input_buffer", boldify, "boldify" )}}{%
   \directlua{luatexbase.remove_from_callback (
      "process_input_buffer", "boldify" )}}

\begin{document}

\begin{boldify}

pizza: Italian food made of \dots
dog: animal that is similar to a wolf and \dots
John: name of person that \dots
plastic: type of material given by \dots

\end{boldify}

aaa: 

bbb:

\end{document}
9
  • I think there is a spurious space at the start of the lines that contain :
    – Udi Fogiel
    Commented Oct 26, 2023 at 6:15
  • @UdiFogiel - Thanks. I'm not sure if this whitespace qualifies as "spurious"; it's caused by \parindent not being equal to 0pt, and it's present in the lines that start with "aaa" and "bbb" as well. (FWIW, this issue is also present in David Carlisle's answer.) The indentation may or may not be to the OP's liking. In case it isn't, it can be taken care of by inserting the instruction \setlength\parindent{0pt} immediately after \obeylines.
    – Mico
    Commented Oct 26, 2023 at 6:30
  • I'm comparing the lines in the environment relative to the text outside the environment, both have the same \parindent. Try removing : from the second line, or notice the text outside the environment...
    – Udi Fogiel
    Commented Oct 26, 2023 at 6:34
  • In David's answer, the aaa: is aligned with Jhon:
    – Udi Fogiel
    Commented Oct 26, 2023 at 6:38
  • 1
    Mico, the space at the start of lines is not discarded. I don't know if this is desired, but if not I think the return value of boldify should be s:gsub ( "^%s*(.-):" , "\\textbf{%1}:" )
    – Udi Fogiel
    Commented Oct 26, 2023 at 8:44

You must log in to answer this question.

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