---
title: "Code Languages Guide for Less Likely"
format:
  html:
    include-in-header:
      text: |
        <meta name="robots" content="noindex, nofollow">
    toc: true
execute:
  eval: false
---

This guide shows how to use Stata, Julia, MATLAB, and pseudocode in your Quarto posts.

## Supported Languages

Your site now supports syntax highlighting and execution for:

- **R** (via knitr)
- **Python** (via Jupyter)
- **Julia** (via Jupyter)
- **Stata** (via Stata engine)
- **MATLAB** (via Jupyter MATLAB kernel or syntax highlighting only)
- **Pseudocode** (syntax highlighting only)
- **Observable JS**
- And many more for syntax highlighting only

## Stata Code Blocks

### Syntax Highlighting Only (No Execution)

```stata
* Load data
use "mydata.dta", clear

* Descriptive statistics
summarize age weight height

* Linear regression
regress weight height age

* Save results
estimates store model1
```

### Executed Stata Code (requires Stata installation)

In your post YAML header:

```yaml
---
title: "My Analysis"
format:
  html:
    code-fold: false
engine: knitr
---
```

Then in your post:

````markdown
```{stata, statapath="/Applications/StataNow/StataMP.app/Contents/MacOS/StataMP"}
* Your Stata code here
sysuse auto
summarize price mpg
regress price mpg weight
```
````

### Stata with RStata Package

For executing Stata from R chunks:

````markdown
```{r}
#| label: stata-analysis
#| engine: stata
#| engine.path: "/Applications/Stata/StataSE.app/Contents/MacOS/stata-se"

sysuse auto
summarize price mpg
```
````

## Julia Code Blocks

### Syntax Highlighting Only

```julia
# Julia example
using DataFrames, Statistics

# Create a DataFrame
df = DataFrame(
    x = 1:10,
    y = rand(10)
)

# Calculate statistics
mean(df.y)
std(df.y)

# Linear model
using GLM
model = lm(@formula(y ~ x), df)
```

### Executed Julia Code (requires Julia + IJulia)

In your post YAML:

```yaml
---
title: "Julia Analysis"
jupyter: julia-1.9
---
```

Then in your post:

````markdown
```julia
using Statistics
data = [1, 2, 3, 4, 5]
println("Mean: ", mean(data))
println("Std: ", std(data))
```
````

## MATLAB Code Blocks

### Syntax Highlighting Only

```matlab
% MATLAB example
data = randn(100, 1);

% Descriptive statistics
mu    = mean(data);
sigma = std(data);
fprintf('Mean: %.4f, SD: %.4f\n', mu, sigma);

% Linear regression
X = [ones(100,1), (1:100)'];
y = 2*(1:100)' + randn(100,1);
b = X \ y;   % least-squares solution

% Plot
figure;
scatter(X(:,2), y, 'b.');
hold on;
plot(X(:,2), X*b, 'r-', 'LineWidth', 2);
xlabel('x'); ylabel('y');
title('Simple Linear Regression');
```

### Executed MATLAB Code (requires MATLAB + Jupyter kernel)

Install the MATLAB kernel for Jupyter:

```bash
# From within MATLAB (R2020b+)
# Run: matlab.engine.shareEngine
# Or install the open-source kernel:
pip install matlabengine
```

In your post YAML:

```yaml
---
title: "MATLAB Analysis"
jupyter: matlab
execute:
  cache: true
  freeze: auto
---
```

Then in your post:

````markdown
```{matlab}
% Executed MATLAB code
x = linspace(0, 2*pi, 100);
y = sin(x);
plot(x, y);
title('Sine wave');
```
````

### For MATLAB Posts (YAML header)

```yaml
---
title: "MATLAB Analysis"
format:
  html:
    code-fold: false
jupyter: matlab
execute:
  warning: false
  message: false
---
```

## Pseudocode

Quarto supports several pseudocode formats:

### Method 1: Using Plain Text with Custom Class

````markdown
```{.pseudocode}
ALGORITHM: Linear Regression
INPUT: X (n × p matrix), y (n × 1 vector)
OUTPUT: β (p × 1 coefficient vector)

1. Initialize β = 0
2. REPEAT until convergence:
   a. Calculate residuals: r = y - Xβ
   b. Calculate gradient: g = X'r
   c. Update: β = β + α*g
3. RETURN β
```
````

### Method 2: Using Algorithm Style

```text
Algorithm 1: Bootstrap Confidence Interval
Input: data X, statistic θ̂, confidence level α
Output: CI = [θ_lower, θ_upper]

1: for i = 1 to B do
2:    X* ← resample(X)
3:    θ*_i ← compute_statistic(X*)
4: end for
5: θ_lower ← quantile(θ*, α/2)
6: θ_upper ← quantile(θ*, 1-α/2)
7: return [θ_lower, θ_upper]
```

### Method 3: LaTeX-Style Algorithm (using algorithmic package)

For posts that need formal algorithmic notation, add to YAML:

```yaml
---
title: "My Algorithm Post"
format:
  html:
    include-in-header:
      text: |
        <style>
        .algorithm {
          font-family: "Courier New", monospace;
          background: #f5f5f5;
          padding: 1em;
          border-left: 3px solid #007bff;
        }
        </style>
---
```

Then use:

````markdown
::: {.algorithm}
**Algorithm 1** Maximum Likelihood Estimation

**Input:** Data $X = \{x_1, ..., x_n\}$, likelihood function $L(\theta|X)$

**Output:** $\hat{\theta}_{MLE}$

1. Initialize $\theta_0$
2. **while** not converged **do**
   - Compute $\nabla_\theta \log L(\theta|X)$
   - Update $\theta_{t+1} = \theta_t + \alpha \nabla_\theta \log L(\theta|X)$
   - Check convergence criterion
3. **end while**
4. **return** $\hat{\theta}_{MLE}$
:::
````

## Language-Specific Settings in Post YAML

### For Stata Posts

```yaml
---
title: "Stata Analysis"
format:
  html:
    code-fold: false
engine: knitr
knitr:
  opts_chunk:
    engine: stata
    engine.path: "/Applications/Stata/StataSE.app/Contents/MacOS/stata-se"
---
```

### For Julia Posts

```yaml
---
title: "Julia Analysis"
format:
  html:
    code-fold: false
jupyter: julia-1.9
execute:
  cache: true
  freeze: auto
---
```

### For Mixed Language Posts

```yaml
---
title: "Multi-Language Analysis"
format:
  html:
    code-fold: false
engine: jupyter
kernels:
  - python3
  - julia-1.9
  - ir  # R kernel for Jupyter
---
```

## Syntax Highlighting Themes

Your site uses:
- **Light mode**: Pygments
- **Dark mode**: Monokai

Available themes include:
- pygments, tango, espresso, zenburn, kate, monochrome
- haddock, breezedark, atom-one, github, monokai

To change for a specific post:

```yaml
---
highlight-style:
  light: github
  dark: atom-one
---
```

## Complete Example Post

Here's a complete example using multiple languages:

````markdown
---
title: "Comparing Statistical Methods Across Languages"
author: "Zad Rafi"
date: "2026-01-06"
categories: [statistics, programming]
format:
  html:
    code-fold: false
    code-tools: true
execute:
  warning: false
  message: false
---

## R Analysis

```{r}
# Linear regression in R
data <- data.frame(
  x = 1:10,
  y = 2*1:10 + rnorm(10)
)
model <- lm(y ~ x, data)
summary(model)
```

## Julia Analysis

```julia
# Same analysis in Julia
using GLM, DataFrames
data = DataFrame(x = 1:10, y = 2*(1:10) .+ randn(10))
model = lm(@formula(y ~ x), data)
```

## Stata Analysis

```stata
* Same analysis in Stata
clear
set obs 10
gen x = _n
gen y = 2*x + rnormal()
regress y x
```

## MATLAB Analysis

```matlab
% Same analysis in MATLAB
x = (1:10)';
y = 2*x + randn(10,1);
b = [ones(10,1), x] \ y;
fprintf('Intercept: %.4f, Slope: %.4f\n', b(1), b(2));
```

## Algorithm Pseudocode

```{.pseudocode}
ALGORITHM: Ordinary Least Squares
INPUT: X, y
OUTPUT: β̂

1. Compute X'X
2. Compute (X'X)^(-1)
3. Compute X'y
4. β̂ = (X'X)^(-1) X'y
5. RETURN β̂
```
````

## Installation Requirements

### For Julia Execution
```bash
# Install Julia
# Install IJulia kernel
julia -e 'using Pkg; Pkg.add("IJulia")'
```

### For Stata Execution
```r
# Install RStata package
install.packages("RStata")

# Configure in your R setup chunk
library(RStata)
options("RStata.StataPath" = "/Applications/StataNow/StataMP.app/Contents/MacOS/StataMP")
options("RStata.StataVersion" = 19.5)
```

## Testing Your Setup

1. Create a test post with code blocks
2. Run: `quarto render your-post.qmd`
3. Check the HTML output for proper syntax highlighting
4. If executing code, verify the output appears

## Troubleshooting

**Stata not executing:**
- Check Stata path in options
- Ensure RStata package is installed
- Verify Stata license is active

**Julia not found:**
- Install IJulia: `julia -e 'using Pkg; Pkg.add("IJulia")'`
- Specify correct Julia version in YAML

**Pseudocode not formatting:**
- Use custom CSS styling (see example above)
- Or use plain text/code blocks with descriptive language name

## More Information

- Quarto Code Blocks: <https://quarto.org/docs/computations/execution-options.html>
- Stata in Quarto: <https://quarto.org/docs/computations/stata.html>
- Julia in Quarto: <https://quarto.org/docs/computations/julia.html>
