0

I have a chatlog file (chat.tex), and would like to include different parts of it in different parts of my document.

I can extract the relevant parts I want using regular expressions, or string indexing, for example:

chat.tex

% 2021-12-30 00:00:00
First text of the day

% 2021-12-30 00:00:01
Second text of the day

% 2021-12-30 00:00:02
Third text of the day

% 2021-12-30 00:00:03
Fourth text of the day

I would like to include in one part of the document all text between % 2021-12-30 00:00:01 and % 2021-12-30 00:00:03:


Second text of the day

% 2021-12-30 00:00:02
Third text of the day

Ideally, to prevent multiple read requests to the chat.tex file, its content should be stored as a variable, and one would get the text from that variable.

Code if this was python:

chat = open("chat.tex", "r").read()
def extract(start, end):
  return chat[chat.index(start):chat.index(end)]

extract('% 2021-12-30 00:00:01', '% 2021-12-30 00:00:03')

(This might be partially a duplicate of Parse a file with a regexp and return first match - but I do not understand the answer, and think it might be very outdated, and costly in my scenario. I also do not specifically intend to include a file based on line numbers \input only part of file, without losing SyncTeX support?)

6
  • Simplest way would be to use LuaTeX or PythonTeX and implement it in Lua/Python respectively.
    – user202729
    Commented Dec 30, 2021 at 13:17
  • To understand the answer instead, read interface3 documentation and learn from there. Warning: TeX is highly nontrivial (even expl3), just use Lua/Python instead.
    – user202729
    Commented Dec 30, 2021 at 13:18
  • Thanks @user202729, unfortunately, pythontex does not work on overleaf due to security reasons. I'll attempt this with luatex
    – Amit
    Commented Dec 30, 2021 at 14:04
  • Seems like directlua and luacode are undefined control sequences in overleaf as well
    – Amit
    Commented Dec 30, 2021 at 19:38
  • Need to configure overleaf a bit. E.g.luatex - How to set Overleaf to compile with lualatex --shell-escape <file>? - TeX - LaTeX Stack Exchange (still, even if you can already program in e.g. Python, it's easier to compile locally than learn TeX-the-programming-language.)
    – user202729
    Commented Dec 31, 2021 at 3:12

1 Answer 1

0

Thanks to @user202729 the solution is as follows:

  1. Change the tex engine to lualatex, as this solution depends on some lua code.
  2. Create a lua function to read the file and look for the lines:
-- chat.lua

local f = assert(io.open("chat.tex", "r"))
local chat_file = f:read("*all")
f:close()

function readchat(starts, ends)
  i1 = string.find(chat_file, starts) + string.len(starts)
  i2 = string.find(chat_file, ends) - 2

  sub_content = string.sub(chat_file, i1, i2)

  --- tex.print treats the string as one line...  
  for line in string.gmatch(sub_content,"[^\r\n]*") do
    tex.print(line)
  end
end
  1. Create a latex alias to that function
\newcommand{\readchat}[2]{\directlua{readchat(#1, #2)}}
  1. Include the lua file in the beginning of the document
\directlua{dofile("chat.lua")}
  1. Search for a match
\readchat{"7/1/21T18:11:46"}{"7/2/21T09:44:20"}

You must log in to answer this question.

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