MEXICO CITY — It was a perfect portrait of the delicate relationship between the Mexican president and his protégée.

With the pandemic raging, President Andrés Manuel López Obrador drew his allies in for a photo op. Mask-free and eager to please, they all squeezed in tight — except one: Claudia Sheinbaum, one of his most trusted confidantes.

Ms. Sheinbaum, the mayor of Mexico City, was leery of getting too close. So she stood at the edge of the stage that day in April, a literal outlier, the only person in the room social distancing.

How much space — physical and political — to put between herself and the most powerful man in Mexico is a question that will define Ms. Sheinbaum’s legacy, her political future and the fates of millions of people living in the world’s fifth largest city.

Mr. López Obrador minimized the pandemic early on, questioning the science behind face masks and doing little testing. Seeking to avert economic pain, he has barely restricted travel. Under his watch, Mexico has the fourth highest coronavirus death toll worldwide.

For Ms. Sheinbaum, a scientist with a Ph.D. in energy engineering, staying too close to the president would mean ignoring the practices she knows are in the best interest of public health. Stray too far, and she risks losing the support of a political kingmaker who is said to be considering her — the first woman and first Jewish person elected to lead the nation’s capital — as the party’s next presidential candidate.

So far, her strategy has been to follow the science, while refusing to criticize the president.

“I will not allow this to become a political conflict,” said Ms. Sheinbaum, 58, sitting rigidly at her desk, her voice muffled by a cloth mask. “But I also believe I have a role here in the city, and I’m going to abide by what I believe in.”

Credit…Meghan Dhaliwal for The New York Times

When Mr. López Obrador was still kissing babies at rallies and comparing the virus to the flu, Ms. Sheinbaum was planning for a long pandemic. She pushed an aggressive testing and contact tracing campaign. She set up testing kiosks where people get swabbed for free.

And she required that everyone in Mexico City use face coverings on public transportation, donning a mask every time she addressed the press.

She argues privately with Hugo López-Gatell, the health official tapped by the president to direct the nation’s coronavirus response. But her staff has been instructed to emphasize, in public, how aligned the city and federal governments are and how much they have in common.

“This is how we have always behaved, always respecting, always informing,” she said. “Trying to coordinate as much as possible.”

Ms. Sheinbaum sends her first text message of the day just after 5 a.m., often to one of the wonks on her team who measures progress in taming the outbreak in Mexico City, the worst nationally.

Every morning, she will ask how many people visited hospitals yesterday, how many went home, how many were intubated, how many died. She tracks which neighborhood had parties, how many people wore a mask on the metro, and whether it was actually more of a chin strap.

The virus has thrived in the dense capital, home to nine million people, half of them poor. But while the toll has been horrific — more than 11,000 have died — analysts say it could have been worse without the mayor’s interventions.

Credit…Daniel Berehulak for The New York Times

Early on, Ms. Sheinbaum created a hotline where people could report coronavirus symptoms and receive a free package of masks, a thermometer, antibacterial gel and a pain reliever.

Doctors told her the N95 masks the federal government had imported from China were too narrow to fit Mexican faces, so she converted a local factory into a mask-making operation.

Only around 600 intensive care unit beds were equipped to treat coronavirus patients in the city, so she bought hundreds of ventilators from the United States, Germany and China. There are now more than 2,000 of those I.C.U. beds.

To evaluate where things stand, Ms. Sheinbaum focuses on the number of people admitted to hospitals — and these days, she likes what she sees. When the capital reopened much of its economy on July 1, six in 10 hospital beds were occupied, compared with just four in 10 now.

“What matters to us is that hospitals aren’t overwhelmed,” she said.

The problem with her strategy, epidemiologists say, is that it offers little sense of the virus’s prevalence among young people, who are less likely to go to the hospital. By the time sickened people get to emergency rooms, it is often too late to break the chain of transmission.

“For the two weeks that they were infected prior to ending up in the hospital, they were exposing tens or possibly hundreds of people,” said Dr. Thomas Tsai, of the Harvard Global Health Institute.

The alternative is mass testing, which the city isn’t doing, even after throwing money at the problem and tripling testing rates. Mexico City now does 40 tests per 100,000 residents, compared with just nine per 100,000 in the country as a whole. But that’s still low compared with the 322 per 100,000 in New York, or 130 in Los Angeles.

The share of people who test positive in Mexico City has dropped, but remains around 30 percent, six times the rate considered safe enough by the World Health Organization to reopen the economy.

“This is not Stockholm. This is not Singapore. We have limited resources,” said José Merino, who directs the city agency that helps oversee the coronavirus task force. “And we don’t have a way to prevent people from going out on the street and trying to feed their families.”

Credit…Meghan Dhaliwal for The New York Times

The city would need to spend about a tenth of its annual budget on testing if it wanted to reach New York levels. And the federal government is not helping much. Mr. López-Gatell has said he believes testing everyone is a “waste of time,” part of the reason Mexico’s national testing rates are among the lowest.

Mr. López-Gatell has been criticized for promising an imminent end to the pandemic and projecting only 6,000 deaths early on. There are now more than 65,000.

And yet, Mexico’s president trusts him completely. In meetings with the president, Ms. Sheinbaum said she had “presented scenarios for Mexico City,” and conveyed her belief in masks. “He always told me to come to an agreement with Hugo.”

The task has not been easy for the mayor.

She felt deeply uncomfortable about allowing a concert in the capital in mid-March. Mr. López-Gatell permitted it. He generally addresses the press mask-less and said forcing people to wear masks could lead to “human rights abuses.”

In July, Mr. López-Gatell announced at a news conference that Ms. Sheinbaum had given him a gift: a package of masks. He didn’t put one on.

The mayor said she had engaged in “public, notorious differences” with Mr. López-Gatell, but refuses to question him.

“I will not contradict the Mexican government,” she said.

The daughter of two leftist Jews, Ms. Sheinbaum was raised as an atheist in a Catholic country ruled by the same party for seven decades.

She first met Mr. López Obrador when he visited her house to meet with her now ex-husband, Carlos Imaz, a leftist political leader, and other activists. “I prepared the coffee and the cookies,” she said.

In time, she became one of the country’s top climate researchers, and when Mr. López Obrador became mayor of Mexico City in 2000, he appointed Ms. Sheinbaum as his secretary of the environment.

When, in 2018, on his third attempt, Mr. López Obrador was elected to the highest office on the ticket of the party he had founded, Ms. Sheinbaum ran with his coalition and was voted mayor of Mexico City.

Credit…Fernando Llano/Associated Press

“There’s a very important part of affection and admiration that comes from fighting with someone for two decades, shoulder to shoulder, from the opposition, from having no power, no money, from being sabotaged, from being persecuted,” said Ana Laura Magaloni, a law professor who advised Ms. Sheinbaum’s mayoral campaign. “All of a sudden that group rises to power and it’s like, ‘that history makes us a team.’”

Sitting in her office, in front of a photo of herself and the president, Ms. Sheinbaum wrapped the lanyard attached to an oximeter around her finger. After a staff member tested positive for the virus, she started measuring her oxygen levels multiple times a day.

“The pandemic will end the moment there are vaccines,” said Ms. Sheinbaum, adding, “so if we have differences about whether to wear masks or not, or whether to do more tests or not, that’s minor compared to the transformation of our country.”

Several people said her relationship to Mr. López Obrador was like daughter and father. He “loves her and protects her,” said Marta Lamas, a feminist scholar who advised Ms. Sheinbaum’s campaign. “And she is totally loyal to him and his project.”

But those who have worked with Mr. López Obrador say he can become mistrustful, even of his closest allies.

“A paternal relationship is one where I protect you no matter what, and that’s not the case with Andrés Manuel,” said Paola Ojeda, who worked with Mr. López Obrador when he was mayor and on three of his presidential campaigns.

He won’t choose his successor until the last moment, she said.

“Claudia has earned his respect and support, day by day,” said Ms. Ojeda. “And she knows, like everyone close to him, that she could lose it the moment she does something she shouldn’t.”

Let’s block ads! (Why?)