Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .opencode/opencode.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"$schema": "https://opencode.ai/config.json",
"plugin": [
"oh-my-openagent"
]
}
28 changes: 15 additions & 13 deletions backend/api/latex_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@
"12pt": "\\normalsize",
}

# Spacing presets: (section_before, section_after, subsection_before, subsection_after, formula_spacing)
# Spacing presets: (section_before, section_after, subsection_before, subsection_after, formula_gap, baselineskip)
SPACING_MAP = {
"tiny": ("0pt", "0pt", "0pt", "0pt", "0pt"),
"small": ("2pt", "1pt", "1pt", "0.5pt", "2pt"),
"medium": ("8pt", "4pt", "4pt", "2pt", "8pt"),
"large": ("16pt", "8pt", "8pt", "4pt", "16pt"),
"tiny": ("0pt", "0pt", "0pt", "0pt", "0pt", "4pt"),
"small": ("2pt", "1pt", "1pt", "0.5pt", "2pt", "6pt"),
"medium": ("8pt", "4pt", "4pt", "2pt", "8pt", "10pt"),
"large": ("16pt", "8pt", "8pt", "4pt", "16pt", "14pt"),
}


Expand All @@ -47,7 +47,7 @@ def build_dynamic_header(columns=2, font_size="10pt", margins="0.25in", spacing=
Build a dynamic LaTeX header based on user-selected options.
"""
size_command = FONT_SIZE_MAP.get(font_size, "\\footnotesize")
sec_before, sec_after, subsec_before, subsec_after, _ = SPACING_MAP.get(spacing, SPACING_MAP["large"])
sec_before, sec_after, subsec_before, subsec_after, _, baseline_skip = SPACING_MAP.get(spacing, SPACING_MAP["large"])

# The standard `article` class only supports 10pt/11pt/12pt.
# Use `extarticle` (from the extsizes package) for 8pt and 9pt.
Expand All @@ -70,6 +70,7 @@ def build_dynamic_header(columns=2, font_size="10pt", margins="0.25in", spacing=
"\\titleformat{\\subsection}{\\normalfont\\scriptsize\\bfseries}{}{0pt}{}",
f"\\titlespacing*{{\\section}}{{0pt}}{{{sec_before}}}{{{sec_after}}}",
f"\\titlespacing*{{\\subsection}}{{0pt}}{{{subsec_before}}}{{{subsec_after}}}",
f"\\setlength{{\\baselineskip}}{{{baseline_skip}}}",
"",
"\\begin{document}",
size_command,
Comment on lines 71 to 76
Copy link

Copilot AI Apr 9, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

\setlength{\baselineskip}{...} is set before \begin{document} and before applying the selected font size command. In LaTeX, changing font size (e.g. \footnotesize/\tiny) typically resets \baselineskip, so this line may have no effect. Consider applying the baselineskip after the size command (or using \AtBeginDocument / \fontsize{...}{...}\selectfont) so the spacing preset actually takes effect.

Copilot uses AI. Check for mistakes.
Comment on lines +73 to 76
Copy link

Copilot AI Apr 9, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new baselineskip behavior isn’t covered by existing API tests (e.g., test_generate_sheet_with_spacing only asserts that titlespacing exists). Consider adding an assertion that the generated LaTeX includes the expected \setlength{\baselineskip}{...} for a given spacing preset, so regressions in spacing presets are caught.

Copilot uses AI. Check for mistakes.
Expand Down Expand Up @@ -103,7 +104,7 @@ def build_latex_for_formulas(selected_formulas, columns=2, font_size="10pt", mar
"""
header = build_dynamic_header(columns, font_size, margins, spacing)
footer = build_dynamic_footer(columns)
_, _, _, _, formula_gap = SPACING_MAP.get(spacing, SPACING_MAP["large"])
_, _, _, _, formula_gap, _ = SPACING_MAP.get(spacing, SPACING_MAP["large"])

if not selected_formulas:
return header + footer
Expand All @@ -124,10 +125,10 @@ def build_latex_for_formulas(selected_formulas, columns=2, font_size="10pt", mar
body_lines.append(r"\end{flushleft}")
in_flushleft = False
if current_class is not None:
body_lines.append("")
body_lines.append("%") # Suppress paragraph break between sections
escaped_class = class_name.replace("&", "\\&")
body_lines.append("\\section*{" + escaped_class + "}")
body_lines.append("")
body_lines.append("%") # Suppress paragraph break after section
current_class = class_name
current_category = None

Expand All @@ -139,7 +140,7 @@ def build_latex_for_formulas(selected_formulas, columns=2, font_size="10pt", mar
if not is_special:
escaped_category = category.replace("&", "\\&")
body_lines.append("\\subsection*{" + escaped_category + "}")
body_lines.append("")
body_lines.append("%")
body_lines.append(r"\begin{flushleft}")
in_flushleft = True
current_category = category
Expand All @@ -149,9 +150,10 @@ def build_latex_for_formulas(selected_formulas, columns=2, font_size="10pt", mar
else:
# Escape special LaTeX characters in the formula name
escaped_name = name.replace("\\", "\\textbackslash ").replace("&", "\\&").replace("%", "\\%").replace("#", "\\#").replace("_", "\\_").replace("^", "\\textasciicircum ").replace("{", "\\{").replace("}", "\\}")
body_lines.append("\\textbf{" + escaped_name + "}")
body_lines.append("\\[ \\adjustbox{max width=\\linewidth}{\\displaystyle " + latex + "} \\]")
body_lines.append(f"\\\\[{formula_gap}]")
body_lines.append(r"\textbf{" + escaped_name + "}")
body_lines.append(r"\[" + r" \adjustbox{max width=\linewidth}{$" + latex + r"$} " + r"\]")
if formula_gap != "0pt":
body_lines.append(r"\vspace{" + formula_gap + "}")

if in_flushleft:
body_lines.append(r"\end{flushleft}")
Expand Down
8 changes: 4 additions & 4 deletions backend/api/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,13 +242,13 @@ def test_filter_problems_by_sheet(self, api_client, sample_problem, sample_sheet
class TestGenerateSheetEndpoint:
def test_generate_sheet_no_formulas(self, api_client):
resp = api_client.post("/api/generate-sheet/", {"formulas": []}, format="json")
assert resp.status_code == 400
assert "error" in resp.json()
assert resp.status_code == 200
assert "tex_code" in resp.json()

def test_generate_sheet_missing_formulas_key(self, api_client):
resp = api_client.post("/api/generate-sheet/", {}, format="json")
assert resp.status_code == 400
assert "error" in resp.json()
assert resp.status_code == 200
assert "tex_code" in resp.json()

def test_generate_sheet_valid_formula(self, api_client):
resp = api_client.post(
Expand Down
9 changes: 5 additions & 4 deletions backend/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

def validate_layout_params(columns, font_size, margins, spacing):
try:
columns = max(1, min(3, int(columns)))
columns = max(1, min(4, int(columns)))
except (TypeError, ValueError):
columns = 2

Expand Down Expand Up @@ -72,11 +72,12 @@ def generate_sheet(request):
margins = request.data.get("margins", "0.25in")
spacing = request.data.get("spacing", "large")

if not selected:
return Response({"error": "No formulas selected"}, status=400)

columns, font_size, margins, spacing = validate_layout_params(columns, font_size, margins, spacing)

if not selected:
tex_code = build_latex_for_formulas([], columns, font_size, margins, spacing)
return Response({"tex_code": tex_code})

formula_data = get_formula_data()
selected_formulas = []

Expand Down
9 changes: 5 additions & 4 deletions frontend/src/components/CreateCheatSheet.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,7 @@ const LayoutOptions = ({ columns, setColumns, fontSize, setFontSize, spacing, se
<option value={1}>1 Column</option>
<option value={2}>2 Columns</option>
<option value={3}>3 Columns</option>
<option value={4}>4 Columns</option>
</select>
</div>
<div className="layout-control">
Expand Down Expand Up @@ -601,7 +602,10 @@ const CreateCheatSheet = ({ onSave, initialData }) => {
onRemoveClass={removeClassFromOrder}
onRemoveFormula={removeSingleFormula}
/>
</div>

{/* Box 2: Editor and preview */}
<div className="create-cheat-sheet panel-box">
<LayoutOptions
columns={columns}
setColumns={setColumns}
Expand All @@ -612,10 +616,7 @@ const CreateCheatSheet = ({ onSave, initialData }) => {
margins={margins}
setMargins={setMargins}
/>
</div>

{/* Box 2: Editor and preview */}
<div className="create-cheat-sheet panel-box">

<div className="editor-container">
<LatexEditor
content={content}
Expand Down
62 changes: 60 additions & 2 deletions frontend/src/hooks/latex.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,11 +131,69 @@ export function useLatex(initialData) {
isCompilingRef.current = true;
setIsCompiling(true);
setCompileError(null);

let contentToCompile = content;

try {
const response = await fetch('/api/generate-sheet/', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
formulas: [],
columns: columns,
font_size: fontSize,
spacing: spacing,
margins: margins
}),
});
if (response.ok) {
const data = await response.json();
const newLatex = data.tex_code;

const oldBodyMatch = content.match(/\\begin\{document\}([\s\S]*)\\end\{document\}/);

if (oldBodyMatch) {
const oldBody = oldBodyMatch[1];

const multicolMatch = oldBody.match(/\\begin\{multicols\}\{(\d+)\}([\s\S]*?)\\end\{multicols\}/);

let formulaContent = oldBody;
if (multicolMatch) {
formulaContent = multicolMatch[2];
Comment on lines +158 to +162
Copy link

Copilot AI Apr 9, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The multicolumn body extraction captures everything after the column count, which includes template scaffolding like \raggedcolumns. When you later prepend the regenerated template's own content, this can duplicate \raggedcolumns (and any other boilerplate inside the multicols block), making the merged LaTeX inconsistent. Consider extracting only the user-authored portion (e.g., strip leading \raggedcolumns/blank lines) before reinserting.

Copilot uses AI. Check for mistakes.
}

const newParts = newLatex.split('\\begin{multicols}');
if (newParts.length > 1) {
const afterMulticols = newParts[1];
const columnMatch = afterMulticols.match(/^\{(\d+)\}/);

if (columnMatch) {
const columnCount = columnMatch[1];
const afterColumn = afterMulticols.slice(columnMatch[0].length);

const beforeEnd = afterColumn.split('\\end{multicols}')[0];
const afterEnd = afterColumn.split('\\end{multicols}').slice(1).join('\\end{multicols}');

contentToCompile = newParts[0] + '\\begin{multicols}{' + columnCount + '}' + beforeEnd + formulaContent + '\\end{multicols}' + afterEnd;
} else {
contentToCompile = newLatex;
}
} else {
contentToCompile = newLatex;
}
} else {
contentToCompile = newLatex;
}
}
} catch (e) {
console.log('Regenerate failed, using existing content:', e);
}

try {
const response = await fetch('/api/compile/', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ content }),
body: JSON.stringify({ content: contentToCompile }),
});
if (!response.ok) {
const errorData = await response.json().catch(() => ({}));
Expand Down Expand Up @@ -163,7 +221,7 @@ export function useLatex(initialData) {
setIsCompiling(false);
isCompilingRef.current = false;
}
}, [content]);
}, [content, columns, fontSize, spacing, margins]);

const handlePreview = useCallback(async (latexContent = null, regenerateOptions = null) => {
if (isCompilingRef.current) return;
Expand Down
Loading