* ==============================================================================
* THESIS DIAGRAM GENERATOR - COMPLETE VERSION 6.2.0 FIXED CENTERING
* Author: Wu Lianghai, Anhui University of Technology (AHUT)
*         Ma'anshan, China, E-mail: agd2010@yeah.net
*         Wu Hanyan, School of Economics and Management,
*         Nanjing University of Aeronautics and Astronautics (NUAA)
*         Nanjing, China, E-mail: 2325476320@qq.com
*         Jin Xuening, Anhui University of Technology (AHUT)
*         Ma'anshan, China, E-mail: 1418924481@qq.com
* Version: 6.2.0 Fixed Centering and Positioning
* Date: 09Feb2026
* Description: Generate LaTeX code for thesis framework diagrams with 5 templates
* Supports Chinese and English languages
* ==============================================================================

capture program drop thesis_diagram
program define thesis_diagram
    version 16.0
    syntax , filename(string) [Template(integer 1) Title(string) Author(string) ///
                              University(string) Language(string) SIMple REPLACE]

    * --------------------------------------------------------------------------
    * PARAMETER VALIDATION AND DEFAULT SETTINGS
    * --------------------------------------------------------------------------
    if `template' < 1 | `template' > 5 {
        display as error "Template number must be between 1 and 5"
        exit 198
    }

    if missing("`filename'") {
        local filename "thesis_diagram.tex"
    }

    * Ensure .tex extension
    if !regexm("`filename'", "\.tex$") {
        local filename "`filename'.tex"
    }

    * Language setting (default: English)
    if missing("`language'") {
        local language "english"
    }
    else {
        local language = lower(substr("`language'", 1, 1))
        if "`language'" == "c" | "`language'" == "z" | "`language'" == "中" {
            local language "chinese"
        }
        else {
            local language "english"
        }
    }

    * Set default values based on language
    if missing("`title'") {
        if "`language'" == "chinese" {
            local title "博士学位论文研究框架"
        }
        else {
            local title "Doctoral Dissertation Research Framework"
        }
    }

    * Default author and university (empty by default)
    local show_author = !missing("`author'")
    local show_university = !missing("`university'")

    * --------------------------------------------------------------------------
    * CHECK IF FILE EXISTS AND REPLACE OPTION
    * --------------------------------------------------------------------------
    if "`replace'" != "" & "`replace'" != "r" {
        local replace "replace"
    }

    if "`replace'" == "" {
        capture confirm file "`filename'"
        if !_rc {
            display as error "File `filename' already exists. Use replace option to overwrite."
            exit 602
        }
    }

    * --------------------------------------------------------------------------
    * GENERATE LATEX FILE
    * --------------------------------------------------------------------------
    tempname fh
    file open `fh' using "`filename'", write text replace

    * Write LaTeX document header with proper centering
    file write `fh' "% ==============================================================================" _n
    file write `fh' "% THESIS FRAMEWORK DIAGRAM - FIXED CENTERING VERSION 6.2.0" _n
    file write `fh' "% Generated by thesis_diagram v6.2.0" _n
    file write `fh' "% Date: 09Feb2026" _n
    file write `fh' "% Authors: Wu Lianghai, Wu Hanyan, Jin Xuening" _n
    file write `fh' "% Language: `language'" _n
    file write `fh' "% Template: `template'" _n
    file write `fh' "% Note: Fixed all centering and left truncation issues" _n
    file write `fh' "% ==============================================================================" _n(2)

    if "`language'" == "chinese" {
        file write `fh' "\documentclass[border=5pt]{ctexart}" _n
    }
    else {
        file write `fh' "\documentclass[border=5pt]{standalone}" _n
    }

    file write `fh' "\usepackage{tikz}" _n
    file write `fh' "\usetikzlibrary{shapes.geometric, arrows.meta, positioning, calc, backgrounds}" _n
    file write `fh' "\usepackage{xcolor}" _n
    file write `fh' "\usepackage{amsmath}" _n
    file write `fh' "\usepackage{fontspec}" _n
    file write `fh' "\usepackage[paper=a4paper, margin=25mm]{geometry}" _n  // Increased margins to 25mm
    if "`language'" == "chinese" {
        file write `fh' "\usepackage[slantfont, boldfont]{xeCJK}" _n
    }
    file write `fh' "" _n

    file write `fh' "\begin{document}" _n
    file write `fh' "\centering" _n
    file write `fh' "\pagestyle{empty}" _n  // Remove page numbers
    file write `fh' "" _n

    * --------------------------------------------------------------------------
    * SELECT AND GENERATE TEMPLATE
    * --------------------------------------------------------------------------
    if `template' == 1 {
        template1_final `fh' "`title'" "`author'" "`university'" "`language'" "`show_author'" "`show_university'"
    }
    else if `template' == 2 {
        template2_final `fh' "`title'" "`author'" "`university'" "`language'" "`show_author'" "`show_university'" "`simple'"
    }
    else if `template' == 3 {
        template3_final `fh' "`title'" "`author'" "`university'" "`language'" "`show_author'" "`show_university'" "`simple'"
    }
    else if `template' == 4 {
        template4_final `fh' "`title'" "`author'" "`university'" "`language'" "`show_author'" "`show_university'"
    }
    else if `template' == 5 {
        template5_final `fh' "`title'" "`author'" "`university'" "`language'" "`show_author'" "`show_university'"
    }

    file write `fh' "\end{document}" _n

    file close `fh'

    * --------------------------------------------------------------------------
    * DISPLAY SUCCESS MESSAGE
    * --------------------------------------------------------------------------
    display as text _n "{hline 70}"
    display as text "{bf:SUCCESS: Thesis framework diagram generated!}" _n
    display as text "File saved to: {bf:`filename'}" _n
    display as text "Template: `template' | Language: `language'" _n
    if `template' == 3 {
        display as text "{bf:Note: Template 3 now has fixed centering}" _n
    }
    if `template' == 4 {
        display as text "{bf:Note: Template 4 now has fixed centering}" _n
    }
    display as text "{hline 35}" _n
    display as text "{bf:COMPILE COMMANDS:}" _n
    if "`language'" == "chinese" {
        display as text "1. {stata xelatex --interaction=nonstopmode `filename'}" _n
        display as text "2. {stata lualatex --interaction=nonstopmode `filename'}" _n
    }
    else {
        display as text "1. {stata pdflatex --interaction=nonstopmode `filename'}" _n
        display as text "2. {stata xelatex --interaction=nonstopmode `filename'}" _n
    }
    display as text "{hline 35}" _n
    display as text "Developed by: Wu Lianghai, Wu Hanyan, Jin Xuening" _n
    display as text "Contact: {browse mailto:agd2010@yeah.net:agd2010@yeah.net}" _n
    display as text "{hline 70}" _n

end

* ==============================================================================
* TEMPLATE 1: CLASSIC HIERARCHICAL STRUCTURE (FINAL VERSION)
* ==============================================================================
program define template1_final
    args fh title author university language show_author show_university

    file write `fh' "\begin{tikzpicture}[>=stealth, node distance=1.2cm, scale=0.8, transform shape]" _n(2)

    * Title section
    file write `fh' "    % Title section - CENTERED" _n
    file write `fh' "    \node[rectangle, text width=12cm, align=center, font=\Large\bfseries] (title) at (0,0) {" _n
    file write `fh' "        `title'" _n
    if `show_author' | `show_university' {
        file write `fh' "        \\[4pt]" _n
        if `show_author' & `show_university' {
            file write `fh' "        \normalsize `author' | `university'" _n
        }
        else if `show_author' {
            file write `fh' "        \normalsize `author'" _n
        }
        else if `show_university' {
            file write `fh' "        \normalsize `university'" _n
        }
    }
    file write `fh' "    };" _n(2)

    if "`language'" == "chinese" {
        * Chinese version
        file write `fh' "    % Main research topic" _n
        file write `fh' "    \node[rectangle, draw=blue, fill=blue!10, thick, rounded corners," _n
        file write `fh' "          text width=10cm, align=center, minimum height=1.2cm, below=1.2cm of title," _n
        file write `fh' "          font=\large] (main) {研究主题: 分析框架};" _n(2)

        file write `fh' "    % Research sections" _n
        file write `fh' "    \node[rectangle, draw=green, fill=green!10, rounded corners," _n
        file write `fh' "          text width=9cm, align=center, minimum height=1cm, below=of main] (part1) {第一章: 研究背景与意义};" _n

        file write `fh' "    \node[rectangle, draw=green, fill=green!10, rounded corners," _n
        file write `fh' "          text width=9cm, align=center, minimum height=1cm, below=of part1] (part2) {第二章: 文献综述};" _n

        file write `fh' "    \node[rectangle, draw=green, fill=green!10, rounded corners," _n
        file write `fh' "          text width=9cm, align=center, minimum height=1cm, below=of part2] (part3) {第三章: 研究方法};" _n

        file write `fh' "    \node[rectangle, draw=green, fill=green!10, rounded corners," _n
        file write `fh' "          text width=9cm, align=center, minimum height=1cm, below=of part3] (part4) {第四章: 实证分析};" _n

        file write `fh' "    \node[rectangle, draw=green, fill=green!10, rounded corners," _n
        file write `fh' "          text width=9cm, align=center, minimum height=1cm, below=of part4] (part5) {第五章: 结论与建议};" _n(2)
    }
    else {
        * English version - centered
        file write `fh' "    % Main research topic" _n
        file write `fh' "    \node[rectangle, draw=blue, fill=blue!10, thick, rounded corners," _n
        file write `fh' "          text width=10cm, align=center, minimum height=1.2cm, below=1.2cm of title," _n
        file write `fh' "          font=\large] (main) {Research Topic: Analytical Framework};" _n(2)

        file write `fh' "    % Research sections" _n
        file write `fh' "    \node[rectangle, draw=green, fill=green!10, rounded corners," _n
        file write `fh' "          text width=9cm, align=center, minimum height=1cm, below=of main] (part1) {Chapter 1: Introduction};" _n

        file write `fh' "    \node[rectangle, draw=green, fill=green!10, rounded corners," _n
        file write `fh' "          text width=9cm, align=center, minimum height=1cm, below=of part1] (part2) {Chapter 2: Literature Review};" _n

        file write `fh' "    \node[rectangle, draw=green, fill=green!10, rounded corners," _n
        file write `fh' "          text width=9cm, align=center, minimum height=1cm, below=of part2] (part3) {Chapter 3: Methodology};" _n

        file write `fh' "    \node[rectangle, draw=green, fill=green!10, rounded corners," _n
        file write `fh' "          text width=9cm, align=center, minimum height=1cm, below=of part3] (part4) {Chapter 4: Empirical Analysis};" _n

        file write `fh' "    \node[rectangle, draw=green, fill=green!10, rounded corners," _n
        file write `fh' "          text width=9cm, align=center, minimum height=1cm, below=of part4] (part5) {Chapter 5: Conclusion};" _n(2)
    }

    file write `fh' "    % Connection arrows" _n
    file write `fh' "    \draw[->, thick, blue] (main) -- (part1);" _n
    file write `fh' "    \draw[->, thick, blue] (part1) -- (part2);" _n
    file write `fh' "    \draw[->, thick, blue] (part2) -- (part3);" _n
    file write `fh' "    \draw[->, thick, blue] (part3) -- (part4);" _n
    file write `fh' "    \draw[->, thick, blue] (part4) -- (part5);" _n(2)

    * Optional methods sidebar
    if "`language'" == "chinese" {
        file write `fh' "    % Methods sidebar - ADJUSTED POSITION" _n
        file write `fh' "    \node[rectangle, draw=orange, fill=orange!10, thick, rounded corners," _n
        file write `fh' "          text width=3.5cm, align=left, minimum height=4.5cm, left=0.5cm of part3] (methods) {" _n
        file write `fh' "        \textbf{研究方法:}\\" _n
        file write `fh' "        - 定量分析\\" _n
        file write `fh' "        - 定性分析\\" _n
        file write `fh' "        - 混合方法" _n
        file write `fh' "    };" _n(2)
    }
    else {
        file write `fh' "    % Methods sidebar - ADJUSTED POSITION" _n
        file write `fh' "    \node[rectangle, draw=orange, fill=orange!10, thick, rounded corners," _n
        file write `fh' "          text width=3.5cm, align=left, minimum height=4.5cm, right=0.5cm of part3] (methods) {" _n
        file write `fh' "        \textbf{Methods:}\\" _n
        file write `fh' "        - Quantitative\\" _n
        file write `fh' "        - Qualitative\\" _n
        file write `fh' "        - Mixed" _n
        file write `fh' "    };" _n(2)
    }

    file write `fh' "    \draw[->, dashed, orange, thick] (part3) -- (methods);" _n
    file write `fh' "    \draw[->, dashed, orange, thick] (methods) -- (part4);" _n(2)

    * Figure caption
    file write `fh' "    % Figure caption - CENTERED" _n
    file write `fh' "    \node[below=0.8cm of part5, text width=12cm, align=center, font=\small\bfseries] (caption) at (0,-7) {" _n
    if "`language'" == "chinese" {
        file write `fh' "        图1: 研究框架图" _n
    }
    else {
        file write `fh' "        Figure 1: Research Framework Diagram" _n
    }
    file write `fh' "    };" _n(2)

    file write `fh' "\end{tikzpicture}" _n

end

* ==============================================================================
* TEMPLATE 2: FLOWCHART DIAGRAM - FIXED CAPTION POSITION
* ==============================================================================
program define template2_final
    args fh title author university language show_author show_university simple

    file write `fh' "\begin{tikzpicture}[>=stealth, node distance=1cm, scale=0.75, transform shape]" _n(2)

    * Title section
    file write `fh' "    % Title section - CENTERED" _n
    file write `fh' "    \node[rectangle, text width=12cm, align=center, font=\Large\bfseries] (title) at (0,0) {" _n
    file write `fh' "        `title'" _n
    if `show_author' | `show_university' {
        file write `fh' "        \\[4pt]" _n
        if `show_author' & `show_university' {
            file write `fh' "        \normalsize `author' | `university'" _n
        }
        else if `show_author' {
            file write `fh' "        \normalsize `author'" _n
        }
        else if `show_university' {
            file write `fh' "        \normalsize `university'" _n
        }
    }
    file write `fh' "    };" _n(2)

    if "`language'" == "chinese" {
        * Chinese version
        file write `fh' "    % Research process" _n
        file write `fh' "    \node[rectangle, draw=green, fill=green!10, rounded corners," _n
        file write `fh' "          text width=9cm, align=center, minimum height=1cm, below=1.2cm of title] (step1) {步骤1: 问题提出};" _n

        file write `fh' "    \node[rectangle, draw=green, fill=green!10, rounded corners," _n
        file write `fh' "          text width=9cm, align=center, minimum height=1cm, below=of step1] (step2) {步骤2: 文献综述};" _n

        file write `fh' "    \node[rectangle, draw=blue, fill=blue!10, rounded corners," _n
        file write `fh' "          text width=9cm, align=center, minimum height=1cm, below=of step2] (step3) {步骤3: 研究设计};" _n

        file write `fh' "    \node[rectangle, draw=blue, fill=blue!10, rounded corners," _n
        file write `fh' "          text width=9cm, align=center, minimum height=1cm, below=of step3] (step4) {步骤4: 数据收集};" _n

        file write `fh' "    \node[rectangle, draw=orange, fill=orange!10, rounded corners," _n
        file write `fh' "          text width=9cm, align=center, minimum height=1cm, below=of step4] (step5) {步骤5: 数据分析};" _n

        file write `fh' "    \node[rectangle, draw=red, fill=red!10, rounded corners," _n
        file write `fh' "          text width=9cm, align=center, minimum height=1cm, below=of step5] (step6) {步骤6: 结果与结论};" _n(2)
    }
    else {
        * English version
        file write `fh' "    % Research process" _n
        file write `fh' "    \node[rectangle, draw=green, fill=green!10, rounded corners," _n
        file write `fh' "          text width=9cm, align=center, minimum height=1cm, below=1.2cm of title] (step1) {Step 1: Problem Identification};" _n

        file write `fh' "    \node[rectangle, draw=green, fill=green!10, rounded corners," _n
        file write `fh' "          text width=9cm, align=center, minimum height=1cm, below=of step1] (step2) {Step 2: Literature Review};" _n

        file write `fh' "    \node[rectangle, draw=blue, fill=blue!10, rounded corners," _n
        file write `fh' "          text width=9cm, align=center, minimum height=1cm, below=of step2] (step3) {Step 3: Research Design};" _n

        file write `fh' "    \node[rectangle, draw=blue, fill=blue!10, rounded corners," _n
        file write `fh' "          text width=9cm, align=center, minimum height=1cm, below=of step3] (step4) {Step 4: Data Collection};" _n

        file write `fh' "    \node[rectangle, draw=orange, fill=orange!10, rounded corners," _n
        file write `fh' "          text width=9cm, align=center, minimum height=1cm, below=of step4] (step5) {Step 5: Data Analysis};" _n

        file write `fh' "    \node[rectangle, draw=red, fill=red!10, rounded corners," _n
        file write `fh' "          text width=9cm, align=center, minimum height=1cm, below=of step5] (step6) {Step 6: Results \& Conclusion};" _n(2)
    }

    file write `fh' "    % Connection arrows" _n
    file write `fh' "    \draw[->, thick, green] (step1) -- (step2);" _n
    file write `fh' "    \draw[->, thick, green] (step2) -- (step3);" _n
    file write `fh' "    \draw[->, thick, blue] (step3) -- (step4);" _n
    file write `fh' "    \draw[->, thick, blue] (step4) -- (step5);" _n
    file write `fh' "    \draw[->, thick, orange] (step5) -- (step6);" _n(2)

    * Decision points (only if not simple mode)
    if missing("`simple'") {
        if "`language'" == "chinese" {
            file write `fh' "    % Decision points - Chinese" _n
            file write `fh' "    \node[diamond, draw=blue, fill=blue!5, aspect=2," _n
            file write `fh' "          text width=2cm, align=center, left=1.8cm of step3] (dec1) {方法合适?};" _n

            file write `fh' "    \node[diamond, draw=orange, fill=orange!5, aspect=2," _n
            file write `fh' "          text width=2cm, align=center, left=1.8cm of step5] (dec2) {结果可靠?};" _n(2)
        }
        else {
            file write `fh' "    % Decision points - English" _n
            file write `fh' "    \node[diamond, draw=blue, fill=blue!5, aspect=2," _n
            file write `fh' "          text width=2cm, align=center, right=1.8cm of step3] (dec1) {Method OK?};" _n

            file write `fh' "    \node[diamond, draw=orange, fill=orange!5, aspect=2," _n
            file write `fh' "          text width=2cm, align=center, right=1.8cm of step5] (dec2) {Results OK?};" _n(2)
        }

        file write `fh' "    % Connection arrows for decision points" _n
        if "`language'" == "chinese" {
            file write `fh' "    \draw[->, dashed, blue] (step3) -- (dec1);" _n
            file write `fh' "    \draw[->, dashed, blue] (dec1) |- node[above, pos=0.3] {No} (step2);" _n
            file write `fh' "    \draw[->, dashed, orange] (step5) -- (dec2);" _n
            file write `fh' "    \draw[->, dashed, orange] (dec2) |- node[above, pos=0.3] {No} (step4);" _n(2)
        }
        else {
            file write `fh' "    \draw[->, dashed, blue] (step3) -- (dec1);" _n
            file write `fh' "    \draw[->, dashed, blue] (dec1) |- node[above, pos=0.3] {No} (step2);" _n
            file write `fh' "    \draw[->, dashed, orange] (step5) -- (dec2);" _n
            file write `fh' "    \draw[->, dashed, orange] (dec2) |- node[above, pos=0.3] {No} (step4);" _n(2)
        }
    }

    * Figure caption - AT THE BOTTOM OF PAGE (FIXED)
    file write `fh' "    % Figure caption - AT THE BOTTOM OF PAGE" _n
    file write `fh' "    \node[text width=12cm, align=center, font=\small\bfseries] (caption) at (0,-14) {" _n  // 将图标题放在页面最下方
    if "`language'" == "chinese" {
        file write `fh' "        图1: 研究流程图" _n
    }
    else {
        file write `fh' "        Figure 1: Research Flowchart" _n
    }
    file write `fh' "    };" _n(2)

    file write `fh' "\end{tikzpicture}" _n

end

* ==============================================================================
* TEMPLATE 3: MODULAR DESIGN - FIXED CENTERING
* ==============================================================================
program define template3_final
    args fh title author university language show_author show_university simple

    file write `fh' "\begin{tikzpicture}[>=stealth, scale=0.7, transform shape]" _n(2)  // 减小scale到0.7

    * Title section - CENTERED
    file write `fh' "    % Title section - CENTERED" _n
    file write `fh' "    \node[rectangle, text width=12cm, align=center, font=\Large\bfseries] (title) at (0,0) {" _n
    file write `fh' "        `title'" _n
    if `show_author' | `show_university' {
        file write `fh' "        \\[8pt]" _n
        if `show_author' & `show_university' {
            file write `fh' "        \normalsize `author' | `university'" _n
        }
        else if `show_author' {
            file write `fh' "        \normalsize `author'" _n
        }
        else if `show_university' {
            file write `fh' "        \normalsize `university'" _n
        }
    }
    file write `fh' "    };" _n(2)

    if "`language'" == "chinese" {
        * Chinese version - 减小模块间距，确保居中
        file write `fh' "    % Four modules - REDUCED SPACING FOR CENTERING" _n
        file write `fh' "    \node[rectangle, draw=blue, fill=blue!10, thick, rounded corners," _n
        file write `fh' "          text width=4.2cm, align=center, minimum height=3.2cm] (theory) at (-3.5,-4) {" _n  // 从-4改为-3.5
        file write `fh' "        \textbf{理论模块}\\" _n
        file write `fh' "        - 理论基础\\" _n
        file write `fh' "        - 文献综述\\" _n
        file write `fh' "        - 研究假设" _n
        file write `fh' "    };" _n

        file write `fh' "    \node[rectangle, draw=green, fill=green!10, thick, rounded corners," _n
        file write `fh' "          text width=4.2cm, align=center, minimum height=3.2cm] (method) at (3.5,-4) {" _n  // 从4改为3.5
        file write `fh' "        \textbf{方法模块}\\" _n
        file write `fh' "        - 研究设计\\" _n
        file write `fh' "        - 数据收集\\" _n
        file write `fh' "        - 模型设定" _n
        file write `fh' "    };" _n

        file write `fh' "    \node[rectangle, draw=orange, fill=orange!10, thick, rounded corners," _n
        file write `fh' "          text width=4.2cm, align=center, minimum height=3.2cm] (empirical) at (-3.5,-7.5) {" _n  // 从-4,-8改为-3.5,-7.5
        file write `fh' "        \textbf{实证模块}\\" _n
        file write `fh' "        - 描述统计\\" _n
        file write `fh' "        - 回归分析\\" _n
        file write `fh' "        - 稳健检验" _n
        file write `fh' "    };" _n

        file write `fh' "    \node[rectangle, draw=red, fill=red!10, thick, rounded corners," _n
        file write `fh' "          text width=4.2cm, align=center, minimum height=3.2cm] (conclusion) at (3.5,-7.5) {" _n  // 从4,-8改为3.5,-7.5
        file write `fh' "        \textbf{结论模块}\\" _n
        file write `fh' "        - 主要发现\\" _n
        file write `fh' "        - 政策建议\\" _n
        file write `fh' "        - 研究局限" _n
        file write `fh' "    };" _n(2)

        * Central research question
        file write `fh' "    % Central research question" _n
        file write `fh' "    \node[circle, draw=purple, fill=purple!10, thick," _n
        file write `fh' "          text width=2.5cm, align=center, minimum size=2.8cm, font=\bfseries] (center) at (0,-5.75) {" _n  // 从0,-6改为0,-5.75
        file write `fh' "        核心研究\\" _n
        file write `fh' "        问题" _n
        file write `fh' "    };" _n(2)
    }
    else {
        * English version - REDUCED SPACING FOR CENTERING
        file write `fh' "    % Four modules - REDUCED SPACING FOR CENTERING" _n
        file write `fh' "    \node[rectangle, draw=blue, fill=blue!10, thick, rounded corners," _n
        file write `fh' "          text width=4.2cm, align=center, minimum height=3.2cm] (theory) at (-3.5,-4) {" _n
        file write `fh' "        \textbf{Theory Module}\\" _n
        file write `fh' "        - Theoretical Foundation\\" _n
        file write `fh' "        - Literature Review\\" _n
        file write `fh' "        - Research Hypotheses" _n
        file write `fh' "    };" _n

        file write `fh' "    \node[rectangle, draw=green, fill=green!10, thick, rounded corners," _n
        file write `fh' "          text width=4.2cm, align=center, minimum height=3.2cm] (method) at (3.5,-4) {" _n
        file write `fh' "        \textbf{Methodology Module}\\" _n
        file write `fh' "        - Research Design\\" _n
        file write `fh' "        - Data Collection\\" _n
        file write `fh' "        - Model Specification" _n
        file write `fh' "    };" _n

        file write `fh' "    \node[rectangle, draw=orange, fill=orange!10, thick, rounded corners," _n
        file write `fh' "          text width=4.2cm, align=center, minimum height=3.2cm] (empirical) at (-3.5,-7.5) {" _n
        file write `fh' "        \textbf{Empirical Module}\\" _n
        file write `fh' "        - Descriptive Statistics\\" _n
        file write `fh' "        - Regression Analysis\\" _n
        file write `fh' "        - Robustness Tests" _n
        file write `fh' "    };" _n

        file write `fh' "    \node[rectangle, draw=red, fill=red!10, thick, rounded corners," _n
        file write `fh' "          text width=4.2cm, align=center, minimum height=3.2cm] (conclusion) at (3.5,-7.5) {" _n
        file write `fh' "        \textbf{Conclusion Module}\\" _n
        file write `fh' "        - Main Findings\\" _n
        file write `fh' "        - Policy Implications\\" _n
        file write `fh' "        - Research Limitations" _n
        file write `fh' "    };" _n(2)

        * Central research question
        file write `fh' "    % Central research question" _n
        file write `fh' "    \node[circle, draw=purple, fill=purple!10, thick," _n
        file write `fh' "          text width=2.5cm, align=center, minimum size=2.8cm, font=\bfseries] (center) at (0,-5.75) {" _n
        file write `fh' "        Core Research\\" _n
        file write `fh' "        Question" _n
        file write `fh' "    };" _n(2)
    }

    * Connections between modules
    file write `fh' "    % Connections between modules" _n
    file write `fh' "    \draw[->, thick, blue] (theory) -- (method);" _n
    file write `fh' "    \draw[->, thick, blue] (theory) -- (empirical);" _n
    file write `fh' "    \draw[->, thick, green] (method) -- (empirical);" _n
    file write `fh' "    \draw[->, thick, orange] (empirical) -- (conclusion);" _n(2)

    * Connections to center
    file write `fh' "    % Connections to center" _n
    file write `fh' "    \draw[<->, thick, purple, dashed] (center) -- (theory);" _n
    file write `fh' "    \draw[<->, thick, purple, dashed] (center) -- (method);" _n
    file write `fh' "    \draw[<->, thick, purple, dashed] (center) -- (empirical);" _n
    file write `fh' "    \draw[<->, thick, purple, dashed] (center) -- (conclusion);" _n(2)

    * Timeline (only if not simple mode)
    if missing("`simple'") {
        if "`language'" == "chinese" {
            file write `fh' "    % Timeline" _n
            file write `fh' "    \node[rectangle, draw=gray, fill=gray!5, rounded corners," _n
            file write `fh' "          text width=8cm, align=center, below=0.8cm of empirical] (timeline) {" _n  // 从9cm改为8cm
            file write `fh' "        \textbf{研究时间线:} 第一年 $\rightarrow$ 第二年 $\rightarrow$ 第三年 $\rightarrow$ 第四年" _n
            file write `fh' "    };" _n(2)
        }
        else {
            file write `fh' "    % Timeline" _n
            file write `fh' "    \node[rectangle, draw=gray, fill=gray!5, rounded corners," _n
            file write `fh' "          text width=8cm, align=center, below=0.8cm of empirical] (timeline) {" _n  // 从9cm改为8cm
            file write `fh' "        \textbf{Research Timeline:} Year 1 $\rightarrow$ Year 2 $\rightarrow$ Year 3 $\rightarrow$ Year 4" _n
            file write `fh' "    };" _n(2)
        }
    }

    * Figure caption - CENTERED
    file write `fh' "    % Figure caption - CENTERED" _n
    if missing("`simple'") {
        file write `fh' "    \node[below=0.8cm of timeline, text width=12cm, align=center, font=\small\bfseries] (caption) at (0,-11) {" _n
    }
    else {
        file write `fh' "    \node[below=0.8cm of conclusion.south, text width=12cm, align=center, font=\small\bfseries] (caption) at (0,-9.5) {" _n  // 从-10改为-9.5
    }
    if "`language'" == "chinese" {
        file write `fh' "        图1: 模块化研究设计框架" _n
    }
    else {
        file write `fh' "        Figure 1: Modular Research Design Framework" _n
    }
    file write `fh' "    };" _n(2)

    file write `fh' "\end{tikzpicture}" _n

end

* ==============================================================================
* TEMPLATE 4: THREE-COLUMN LAYOUT - FIXED CENTERING
* ==============================================================================
program define template4_final
    args fh title author university language show_author show_university

    file write `fh' "\begin{tikzpicture}[>=stealth, node distance=0.3cm and 0.5cm, every node/.style={align=center}, scale=0.65, transform shape]" _n(2)  // 进一步减小scale到0.65

    * Main title - PROPERLY CENTERED AND ADJUSTED
    file write `fh' "    % Main title - ADJUSTED FOR CENTERING" _n
    file write `fh' "    \node[rectangle, text width=13cm, align=center, font=\LARGE\bfseries] (maintitle) at (0,9) {" _n  // 从10改为9，宽度从14cm改为13cm
    if "`language'" == "chinese" {
        file write `fh' "        \textbf{双碳战略与企业社会责任耦合研究}" _n
    }
    else {
        file write `fh' "        \textbf{Research on the Coupling of 'Dual Carbon' Strategy and Corporate Social Responsibility}" _n
    }
    file write `fh' "    };" _n(2)

    * Author and university info
    if `show_author' | `show_university' {
        file write `fh' "    \node[below=0.5cm of maintitle, font=\normalsize] (authorinfo) {" _n
        if `show_author' & `show_university' {
            file write `fh' "        `author' | `university'" _n
        }
        else if `show_author' {
            file write `fh' "        `author'" _n
        }
        else if `show_university' {
            file write `fh' "        `university'" _n
        }
        file write `fh' "    };" _n(2)
    }

    * =========================================================================
    * LEFT COLUMN: LITERATURE REVIEW - ADJUSTED FOR CENTERING
    * =========================================================================
    file write `fh' "    % Left column - Literature Review - ADJUSTED FOR CENTERING" _n
    file write `fh' "    \node[rectangle, draw=blue!80!black, fill=blue!5, thick, rounded corners," _n
    file write `fh' "          minimum width=4.2cm, minimum height=13cm, anchor=north] (leftcol) at (-4.5,7) {};" _n  // 从-5,8改为-4.5,7，减小高度
    file write `fh' "    \node[font=\large\bfseries, above=0.1cm of leftcol.north] (lefttitle) {" _n
    if "`language'" == "chinese" {
        file write `fh' "        文献综述" _n
    }
    else {
        file write `fh' "        Literature Review" _n
    }
    file write `fh' "    };" _n
    file write `fh' "" _n

    * Left column content
    file write `fh' "    % Left column content" _n
    local ypos 6.3
    local i 1
    if "`language'" == "chinese" {
        foreach item in {"双碳战略内涵与效应" "企业社会责任内涵与特征" "双碳战略下企业社会责任拓展" "文献研究方法"} {
            file write `fh' "    \node[rectangle, draw=blue!60, fill=blue!3, rounded corners, text width=3.8cm," _n
            file write `fh' "          minimum height=1.4cm, font=\scriptsize] (left`i') at (-4.5,`ypos') {" _n  // 从-5改为-4.5
            file write `fh' "        \textbf{`item'}" _n
            file write `fh' "    };" _n
            local ypos = `ypos' - 1.6  // 从1.8改为1.6
            local i = `i' + 1
        }
    }
    else {
        foreach item in {"Dual Carbon Strategy\\Connotation \\& Effects" "CSR Connotation\\ \\& Characteristics" "CSR Extension under\\Dual Carbon Strategy" "Literature Review\\Methods"} {
            file write `fh' "    \node[rectangle, draw=blue!60, fill=blue!3, rounded corners, text width=3.8cm," _n
            file write `fh' "          minimum height=1.4cm, font=\scriptsize] (left`i') at (-4.5,`ypos') {" _n  // 从-5改为-4.5
            file write `fh' "        \textbf{`item'}" _n
            file write `fh' "    };" _n
            local ypos = `ypos' - 1.6  // 从1.8改为1.6
            local i = `i' + 1
        }
    }
    file write `fh' "" _n

    * =========================================================================
    * CENTER COLUMN: RESEARCH QUESTIONS - ADJUSTED FOR CENTERING
    * =========================================================================
    file write `fh' "    % Center column - Research Questions - ADJUSTED FOR CENTERING" _n
    file write `fh' "    \node[rectangle, draw=green!80!black, fill=green!5, thick, rounded corners," _n
    file write `fh' "          minimum width=4.5cm, minimum height=13cm, anchor=north] (centercol) at (0,7) {};" _n  // 从0,8改为0,7，减小高度
    file write `fh' "    \node[font=\large\bfseries, above=0.1cm of centercol.north] (centertitle) {" _n
    if "`language'" == "chinese" {
        file write `fh' "        研究问题" _n
    }
    else {
        file write `fh' "        Research Questions" _n
    }
    file write `fh' "    };" _n
    file write `fh' "" _n

    * Center column content - 调整垂直位置
    file write `fh' "    % Center column - Three main questions - ADJUSTED POSITIONS" _n

    * Question 1: What
    file write `fh' "    \node[rectangle, draw=red!70, fill=red!5, thick, rounded corners, text width=4cm," _n
    file write `fh' "          minimum height=1.6cm, font=\small\bfseries] (q1) at (0,5) {" _n  // 从5.8改为5
    if "`language'" == "chinese" {
        file write `fh' "        \textbf{是什么？}\\" _n
        file write `fh' "        内涵与拓展" _n
    }
    else {
        file write `fh' "        \textbf{What?}\\" _n
        file write `fh' "        Connotation and Extension" _n
    }
    file write `fh' "    };" _n

    * Question 2: Why
    file write `fh' "    \node[rectangle, draw=orange!70, fill=orange!5, thick, rounded corners, text width=4cm," _n
    file write `fh' "          minimum height=1.6cm, font=\small\bfseries] (q2) at (0,2.2) {" _n  // 从2.5改为2.2
    if "`language'" == "chinese" {
        file write `fh' "        \textbf{为什么？}\\" _n
        file write `fh' "        内生机理" _n
    }
    else {
        file write `fh' "        \textbf{Why?}\\" _n
        file write `fh' "        Endogenous Motivation" _n
    }
    file write `fh' "    };" _n

    * Question 3: How
    file write `fh' "    \node[rectangle, draw=purple!70, fill=purple!5, thick, rounded corners, text width=4cm," _n
    file write `fh' "          minimum height=1.6cm, font=\small\bfseries] (q3) at (0,-0.6) {" _n  // 从-0.8改为-0.6
    if "`language'" == "chinese" {
        file write `fh' "        \textbf{怎么样？}\\" _n
        file write `fh' "        实现路径" _n
    }
    else {
        file write `fh' "        \textbf{How?}\\" _n
        file write `fh' "        Implementation Paths" _n
    }
    file write `fh' "    };" _n

    * Arrows between questions
    file write `fh' "    % Arrows between questions" _n
    file write `fh' "    \draw[->, thick, black, line width=1.5pt] (q1) -- (q2);" _n
    file write `fh' "    \draw[->, thick, black, line width=1.5pt] (q2) -- (q3);" _n(2)

    * =========================================================================
    * RIGHT COLUMN: RESEARCH METHODS - ADJUSTED FOR CENTERING
    * =========================================================================
    file write `fh' "    % Right column - Research Methods - ADJUSTED FOR CENTERING" _n
    file write `fh' "    \node[rectangle, draw=orange!80!black, fill=orange!5, thick, rounded corners," _n
    file write `fh' "          minimum width=4.2cm, minimum height=13cm, anchor=north] (rightcol) at (4.5,7) {};" _n  // 从5,8改为4.5,7
    file write `fh' "    \node[font=\large\bfseries, above=0.1cm of rightcol.north] (righttitle) {" _n
    if "`language'" == "chinese" {
        file write `fh' "        研究方法" _n
    }
    else {
        file write `fh' "        Research Methods" _n
    }
    file write `fh' "    };" _n
    file write `fh' "" _n

    * Right column content
    file write `fh' "    % Right column content" _n
    local ypos 6.3
    local i 1
    if "`language'" == "chinese" {
        foreach item in {"文献考据法" "结构分析法" "机理分析法" "类型学方法" "多元回归分析" "内容分析法"} {
            file write `fh' "    \node[rectangle, draw=orange!60, fill=orange!3, rounded corners, text width=3.8cm," _n
            file write `fh' "          minimum height=1.4cm, font=\scriptsize] (right`i') at (4.5,`ypos') {" _n  // 从5改为4.5
            file write `fh' "        \textbf{`item'}" _n
            file write `fh' "    };" _n
            local ypos = `ypos' - 1.6  // 从1.8改为1.6
            local i = `i' + 1
        }
    }
    else {
        foreach item in {"Literature Review\\Methods" "Structural Analysis\\Method" "Mechanism Analysis\\Method" "Typological Method" "Multiple Regression\\Analysis" "Content Analysis\\Method"} {
            file write `fh' "    \node[rectangle, draw=orange!60, fill=orange!3, rounded corners, text width=3.8cm," _n
            file write `fh' "          minimum height=1.4cm, font=\scriptsize] (right`i') at (4.5,`ypos') {" _n  // 从5改为4.5
            file write `fh' "        \textbf{`item'}" _n
            file write `fh' "    };" _n
            local ypos = `ypos' - 1.6  // 从1.8改为1.6
            local i = `i' + 1
        }
    }
    file write `fh' "" _n

    * =========================================================================
    * CONNECTIONS BETWEEN COLUMNS
    * =========================================================================
    file write `fh' "    % Connections between columns" _n
    file write `fh' "    \draw[->, thin, blue!50, dashed] (leftcol.east) -- (q1.west);" _n
    file write `fh' "    \draw[->, thin, blue!50, dashed] (leftcol.east) -- (q2.west);" _n
    file write `fh' "    \draw[->, thin, blue!50, dashed] (leftcol.east) -- (q3.west);" _n

    file write `fh' "    \draw[->, thin, orange!50, dashed] (q1.east) -- (rightcol.west);" _n
    file write `fh' "    \draw[->, thin, orange!50, dashed] (q2.east) -- (rightcol.west);" _n
    file write `fh' "    \draw[->, thin, orange!50, dashed] (q3.east) -- (rightcol.west);" _n(2)

    * =========================================================================
    * FIGURE CAPTION - PROPERLY CENTERED
    * =========================================================================
    file write `fh' "    % Figure caption - PROPERLY CENTERED" _n
    file write `fh' "    \node[below=1cm of centercol.south, text width=13cm, align=center, font=\small\bfseries] (caption) at (0,-6) {" _n  // 从14cm改为13cm
    if "`language'" == "chinese" {
        file write `fh' "        图1: 三栏研究框架图" _n
    }
    else {
        file write `fh' "        Figure 1: Three-Column Research Framework" _n
    }
    file write `fh' "    };" _n(2)

    file write `fh' "\end{tikzpicture}" _n

end

* ==============================================================================
* TEMPLATE 5: LOGICAL THINKING DIAGRAM - FINAL VERSION
* ==============================================================================
program define template5_final
    args fh title author university language show_author show_university

    file write `fh' "\begin{tikzpicture}[>=stealth, node distance=0.5cm and 1cm, every node/.style={align=center}, scale=0.75, transform shape]" _n(2)

    * Main title - TOP
    file write `fh' "    % Main title - TOP" _n
    file write `fh' "    \node[rectangle, text width=12cm, align=center, font=\Large\bfseries] (maintitle) at (0,12) {" _n
    file write `fh' "        \textbf{双碳战略与企业社会责任耦合机制研究}" _n
    file write `fh' "    };" _n(2)

    * Author and university info
    if `show_author' | `show_university' {
        file write `fh' "    \node[below=0.5cm of maintitle, font=\normalsize] (authorinfo) {" _n
        if `show_author' & `show_university' {
            file write `fh' "        `author' | `university'" _n
        }
        else if `show_author' {
            file write `fh' "        `author'" _n
        }
        else if `show_university' {
            file write `fh' "        `university'" _n
        }
        file write `fh' "    };" _n(2)
    }

    * =========================================================================
    * LEVEL 1: WHAT - TOP SECTION
    * =========================================================================
    file write `fh' "    % Level 1: What - TOP SECTION" _n
    file write `fh' "    \node[rectangle, draw=blue!80!black, fill=blue!10, thick, rounded corners," _n
    file write `fh' "          text width=10cm, align=center, minimum height=1.8cm, font=\large\bfseries] (what) at (0,9) {" _n
    file write `fh' "        \textbf{是什么：双碳战略与企业社会责任的内涵与拓展}" _n
    file write `fh' "    };" _n(2)

    * What sub-items
    file write `fh' "    % What sub-items" _n
    file write `fh' "    \node[rectangle, draw=blue!60, fill=blue!5, rounded corners, text width=3cm," _n
    file write `fh' "          minimum height=1.2cm, font=\normalsize, below left=0.6cm and -0.8cm of what] (what1) {" _n
    file write `fh' "        \textbf{双碳战略}\\效应" _n
    file write `fh' "    };" _n

    file write `fh' "    \node[rectangle, draw=blue!60, fill=blue!5, rounded corners, text width=3cm," _n
    file write `fh' "          minimum height=1.2cm, font=\normalsize, below=0.6cm of what] (what2) {" _n
    file write `fh' "        \textbf{企业社会责任}\\内涵" _n
    file write `fh' "    };" _n

    file write `fh' "    \node[rectangle, draw=blue!60, fill=blue!5, rounded corners, text width=3cm," _n
    file write `fh' "          minimum height=1.2cm, font=\normalsize, below right=0.6cm and -0.8cm of what] (what3) {" _n
    file write `fh' "        \textbf{双碳下的}\\CSR拓展" _n
    file write `fh' "    };" _n(2)

    * =========================================================================
    * LEVEL 2: WHY - MIDDLE SECTION
    * =========================================================================
    file write `fh' "    % Level 2: Why - MIDDLE SECTION" _n
    file write `fh' "    \node[rectangle, draw=green!80!black, fill=green!10, thick, rounded corners," _n
    file write `fh' "          text width=10cm, align=center, minimum height=1.8cm, font=\large\bfseries] (why) at (0,5.5) {" _n
    file write `fh' "        \textbf{为什么：企业社会责任履行耦合双碳战略的内生机理}" _n
    file write `fh' "    };" _n(2)

    * Why sub-items
    file write `fh' "    % Why sub-items" _n
    file write `fh' "    \node[rectangle, draw=green!60, fill=green!5, rounded corners, text width=4cm," _n
    file write `fh' "          minimum height=1.5cm, font=\small, below left=0.6cm and -0.8cm of why] (why1) {" _n
    file write `fh' "        \textbf{竞争优势理论}\\CSR耦合内生动力" _n
    file write `fh' "    };" _n

    file write `fh' "    \node[rectangle, draw=green!60, fill=green!5, rounded corners, text width=4cm," _n
    file write `fh' "          minimum height=1.5cm, font=\small, below right=0.6cm and -0.8cm of why] (why2) {" _n
    file write `fh' "        \textbf{可持续发展理论}\\CSR耦合内生动力" _n
    file write `fh' "    };" _n(2)

    * =========================================================================
    * LEVEL 3: HOW - IMPLEMENTATION PATHS
    * =========================================================================
    file write `fh' "    % Level 3: How - IMPLEMENTATION PATHS" _n
    file write `fh' "    \node[rectangle, draw=orange!80!black, fill=orange!10, thick, rounded corners," _n
    file write `fh' "          text width=10cm, align=center, minimum height=1.8cm, font=\large\bfseries] (how) at (0,2) {" _n
    file write `fh' "        \textbf{怎么样：双碳战略与企业社会责任耦合的实现路径}" _n
    file write `fh' "    };" _n(2)

    * =========================================================================
    * FIVE ACTION PATHWAYS - BELOW "HOW"
    * =========================================================================

    * Left side: 3 action pathways
    file write `fh' "    % Left side - 3 action pathways" _n
    file write `fh' "    \node[rectangle, draw=orange!60, fill=orange!5, rounded corners, text width=3.5cm," _n
    file write `fh' "          minimum height=1.5cm, font=\small] (path1) at (-5,-0.5) {" _n
    file write `fh' "        \textbf{企业特征}\\作用路径" _n
    file write `fh' "    };" _n

    file write `fh' "    \node[rectangle, draw=orange!60, fill=orange!5, rounded corners, text width=3.5cm," _n
    file write `fh' "          minimum height=1.5cm, font=\small, below=1cm of path1] (path2) {" _n
    file write `fh' "        \textbf{公司治理}\\作用路径" _n
    file write `fh' "    };" _n

    file write `fh' "    \node[rectangle, draw=orange!60, fill=orange!5, rounded corners, text width=3.5cm," _n
    file write `fh' "          minimum height=1.5cm, font=\small, below=1cm of path2] (path3) {" _n
    file write `fh' "        \textbf{行业特征}\\作用路径" _n
    file write `fh' "    };" _n(2)

    * Right side: 2 action pathways
    file write `fh' "    % Right side - 2 action pathways" _n
    file write `fh' "    \node[rectangle, draw=orange!60, fill=orange!5, rounded corners, text width=3.5cm," _n
    file write `fh' "          minimum height=1.5cm, font=\small] (path4) at (5,-0.5) {" _n
    file write `fh' "        \textbf{再融资需求}\\作用路径" _n
    file write `fh' "    };" _n

    file write `fh' "    \node[rectangle, draw=orange!60, fill=orange!5, rounded corners, text width=3.5cm," _n
    file write `fh' "          minimum height=1.5cm, font=\small, below=1cm of path4] (path5) {" _n
    file write `fh' "        \textbf{企业生命周期}\\作用路径" _n
    file write `fh' "    };" _n(2)

    * =========================================================================
    * POLICY RECOMMENDATIONS - BELOW ACTION PATHWAYS
    * =========================================================================
    file write `fh' "    % Policy Recommendations - BELOW ACTION PATHWAYS" _n
    file write `fh' "    \node[rectangle, draw=red!80!black, fill=red!10, thick, rounded corners," _n
    file write `fh' "          text width=9cm, align=center, minimum height=1.8cm, font=\large\bfseries] (policy) at (0,-8) {" _n
    file write `fh' "        \textbf{政策建议：促进双碳战略与CSR耦合}" _n
    file write `fh' "    };" _n(2)

    * Policy sub-items
    file write `fh' "    % Policy sub-items" _n
    file write `fh' "    \node[rectangle, draw=red!60, fill=red!5, rounded corners, text width=2.8cm," _n
    file write `fh' "          minimum height=1.3cm, font=\small, below left=0.6cm and -0.6cm of policy] (policy1) {" _n
    file write `fh' "        \textbf{政府}\\差异化监管" _n
    file write `fh' "    };" _n

    file write `fh' "    \node[rectangle, draw=red!60, fill=red!5, rounded corners, text width=2.8cm," _n
    file write `fh' "          minimum height=1.3cm, font=\small, below=0.6cm of policy] (policy2) {" _n
    file write `fh' "        \textbf{企业}\\提高治理" _n
    file write `fh' "    };" _n

    file write `fh' "    \node[rectangle, draw=red!60, fill=red!5, rounded corners, text width=2.8cm," _n
    file write `fh' "          minimum height=1.3cm, font=\small, below right=0.6cm and -0.6cm of policy] (policy3) {" _n
    file write `fh' "        \textbf{利益相关者}\\协同推进" _n
    file write `fh' "    };" _n(2)

    * =========================================================================
    * RESEARCH METHODS - BELOW POLICY RECOMMENDATIONS
    * =========================================================================
    file write `fh' "    % Research methods - BELOW POLICY RECOMMENDATIONS" _n
    file write `fh' "    \node[rectangle, draw=gray, fill=gray!5, rounded corners, text width=10cm," _n
    file write `fh' "          minimum height=1.5cm, font=\small, align=center] (methods) at (0,-13) {" _n
    file write `fh' "        \textbf{研究方法：}文献法 | 结构分析 | 机理分析 | 类型学 | 回归分析 | 内容分析 | 归纳法" _n
    file write `fh' "    };" _n(2)

    * =========================================================================
    * FIGURE CAPTION - BOTTOM
    * =========================================================================
    file write `fh' "    % Figure caption - BOTTOM" _n
    file write `fh' "    \node[below=0.8cm of methods, text width=12cm, align=center, font=\small\bfseries] (caption) at (0,-14) {" _n
    file write `fh' "        图1: 逻辑思路研究框架图" _n
    file write `fh' "    };" _n(2)

    * =========================================================================
    * CONNECTION ARROWS
    * =========================================================================

    * Main vertical connections
    file write `fh' "    % Main vertical connections" _n
    file write `fh' "    \draw[->, very thick, blue!60, line width=1.8pt] (what) -- node[above, font=\small] {研究问题1} (why);" _n
    file write `fh' "    \draw[->, very thick, green!60, line width=1.8pt] (why) -- node[right, font=\small] {研究问题2} (how);" _n
    file write `fh' "    \draw[->, very thick, orange!60, line width=1.8pt] (how) -- node[right, font=\small] {研究问题3} (policy);" _n
    file write `fh' "    \draw[->, very thick, red!60, line width=1.8pt] (policy) -- node[right, font=\small] {研究结论} (methods);" _n(2)

    * Connections from what to sub-items
    file write `fh' "    % Connections from what to sub-items" _n
    file write `fh' "    \draw[->, thick, blue!40] (what) -- (what1);" _n
    file write `fh' "    \draw[->, thick, blue!40] (what) -- (what2);" _n
    file write `fh' "    \draw[->, thick, blue!40] (what) -- (what3);" _n

    * Connections from why to sub-items
    file write `fh' "    % Connections from why to sub-items" _n
    file write `fh' "    \draw[->, thick, green!40] (why) -- (why1);" _n
    file write `fh' "    \draw[->, thick, green!40] (why) -- (why2);" _n(2)

    * Connections from how to five action pathways
    file write `fh' "    % Connections from how to five action pathways" _n
    file write `fh' "    \draw[->, thick, orange!50, dashed] (how.south) -- ++(0,-0.3) -| (path1.north);" _n
    file write `fh' "    \draw[->, thick, orange!50, dashed] (how.south) -- ++(0,-0.3) -| (path2.north);" _n
    file write `fh' "    \draw[->, thick, orange!50, dashed] (how.south) -- ++(0,-0.3) -| (path3.north);" _n
    file write `fh' "    \draw[->, thick, orange!50, dashed] (how.south) -- ++(0,-0.3) -| (path4.north);" _n
    file write `fh' "    \draw[->, thick, orange!50, dashed] (how.south) -- ++(0,-0.3) -| (path5.north);" _n(2)

    * Connections from policy to sub-items
    file write `fh' "    % Connections from policy to sub-items" _n
    file write `fh' "    \draw[->, thick, red!40] (policy) -- (policy1);" _n
    file write `fh' "    \draw[->, thick, red!40] (policy) -- (policy2);" _n
    file write `fh' "    \draw[->, thick, red!40] (policy) -- (policy3);" _n(2)

    file write `fh' "\end{tikzpicture}" _n

end

* ==============================================================================
* END OF PROGRAM
* ==============================================================================

display as text "Thesis Diagram Generator v6.2.0 Fixed Centering and Positioning successfully loaded."
display as text "Type: {stata help thesis_diagram} for documentation."