and I would like to make my script reveal when there is a block of text present within the “$$” tags. This script works well when the text is of the form $ some text $
function file_exists(name)
local f = io.open(name, "r")
if f ~= nil then
io.close(f)
return true
else
return false
end
end
function rows_from(file)
if not file_exists(file) then
return {}
end
local rows = {}
for line in io.lines(file) do
rows[#rows + 1] = line
end
return rows
end
function detect_non_indexed_equations(rows,j)
if rows[j]:sub(1,2) == "$" then
for i=j, #rows do
if rows[i]:sub(-2) == "$" then
print("text between $ detected")
break
end
end
end
end
function fetch_equations(start)
local rows = rows_from(file)
for j = start, #rows do
detect_non_indexed_equations(rows,j)
end
end
fetch_equations(1)
But the block is written like
$
some text
$
print("text between $ detected") run twice, and the block is not revealed correctly.
The issue with your script is that it is designed for single-line $ blocks and doesn’t account for multi-line blocks like:
$
some text
$
To handle multi-line blocks, you need to track whether you’re currently inside a block delimited by $ or $, and collect the lines until the block closes. Here’s how you can adjust your script:
Revised Script
-- Function to check if a file exists
function file_exists(name)
local f = io.open(name, "r")
if f ~= nil then
io.close(f)
return true
else
return false
end
end
-- Function to read all lines from a file into a table
function rows_from(file)
if not file_exists(file) then
return {}
end
local rows = {}
for line in io.lines(file) do
rows[#rows + 1] = line
end
return rows
end
-- Function to detect text between block delimiters ($ or $)
function detect_non_indexed_equations(rows)
local inside_block = false
local block_lines = {}
for _, line in ipairs(rows) do
-- Check for opening/closing of blocks
if not inside_block and (line:match("^%$%$") or line:match("^%$")) then
inside_block = true
block_lines = { line } -- Start collecting block lines
elseif inside_block and (line:match("%$%$%s*$") or line:match("%$%s*$")) then
table.insert(block_lines, line) -- Add the closing line
print("Block detected:")
for _, block_line in ipairs(block_lines) do
print(block_line)
end
print("--- End of Block ---")
inside_block = false -- Reset for the next block
elseif inside_block then
table.insert(block_lines, line) -- Collect lines inside the block
end
end
end
-- Main function to fetch equations from a file
function fetch_equations(file)
local rows = rows_from(file)
detect_non_indexed_equations(rows)
end
-- Replace 'your_file.txt' with your actual file name
fetch_equations("your_file.txt")
Explanation of Changes
Tracking Inside a Block:
A variable inside_block is used to determine if we are currently inside a block of $ or $.
Multi-Line Support:
When entering a block, start collecting lines into the block_lines table.
Continue collecting lines until a closing delimiter ($ or $) is found.
Print Detected Block:
When the block ends, print all the lines collected as part of the block.
More Robust Matching:
The script supports both $ and $ as delimiters.
Example Input File (your_file.txt)
$ some text here $
$
multi-line
block of text
$
Some other content
Output
Block detected:
$ some text here $
--- End of Block ---
Block detected:
$
multi-line
block of text
$
--- End of Block ---
This script should work for both single-line and multi-line $ or $ blocks. Let me know if you need further clarification!