In a Corner, President Enrique Peña Nieto of Mexico Punches Back

Thursday, January 26

MEXICO CITY — Hunkered down in the presidential palace, Enrique Peña Nieto, the unpopular leader of Mexico, was besieged on both sides.

The new American president, Donald J. Trump, had just ordered the construction of a border wall between the two countries, and the public outcry in Mexico was deafening. Top cabinet officials, meanwhile, counseled caution, urging Mr. Peña Nieto not to cancel his meeting with Mr. Trump at the White House next week.

For months, though his ratings hovered near the single digits, the worst of any Mexican president in recent history, Mr. Peña Nieto resisted the temptation to saber-rattle, arguing that the relationship with America was simply too important to fall prey to a war of words.

He wanted to give diplomacy one last try. By Thursday morning, the effort had officially failed.

In a blitz of Twitter messages from the two presidents, fired off over the past two days, the first full-blown foreign policy standoff of the Trump administration has taken shape.

The public sparring came after months of simmering tensions between the two men. For decades, the United States and Mexico have expanded their cooperation and increasingly entwined their fortunes. Now the relationship between America and one of its most important allies and trading partners is being rewritten — on Twitter — culminating in a remarkable back-and-forth as the world looked on.

It began with Mr. Trump’s proclamation to build the wall. Next came a diplomatic response from Mr. Peña Nieto, urging unity, accompanied by suggestions from his aides that the meeting might be scrapped over the offense.

Mr. Trump followed on Thursday morning with a threat to cancel the meeting himself. Soon after, Mr. Peña Nieto officially announced that he would not attend, effectively beating Mr. Trump to the punch.

The exchange offered insight into the evolution of Mexico’s president, who began his term with great fanfare in 2012, only to be hounded by scandal, the violence engulfing his nation, a steady decline in the polls and, now, perhaps the worst period in Mexican-American relations since President Calvin Coolidge.

After Mr. Peña Nieto called off the meeting in a Twitter post, Mr. Trump fired back, accusing Mexico of burdening the United States with illegal immigrants, criminals and a trade deficit.

“Most illegal immigration is coming from our southern border,” Mr. Trump said at a Republican retreat. “I’ve said many times that the American people will not pay for the wall, and I’ve made that clear to the government of Mexico.”

Now Mr. Peña Nieto must find a way to preserve his nation’s economic interests while confronting an unpredictable, and at times hostile, American president.

In some respects, Mexico has become a trial run for Mr. Trump’s promise to place America first on the global stage.

In his dealings with Mr. Trump, Mr. Peña Nieto has found himself in a bind: trapped between his own people, who have demanded a vehement response to Mr. Trump’s taunts about Mexico, and a foreign leader who controls much of his country’s destiny.

“Peña Nieto has made a superhuman effort,” said Jesus Silva-Herzog, a professor at the School of Government at Tecnológico de Monterrey. “He has gone above and beyond to preserve the friendship with America and has done everything possible, while risking all of his prestige and popularity, to try to find a common ground of trust with Mr. Trump.”

During the campaign and now as president, Mr. Trump has taken aim at perhaps the most prized possession of Mexico: its image. Throughout his presidency, the Mexican leader has tried to portray his country as a place of economic opportunity, a cultural capital and a nation rising on the world stage. Mr. Trump has sought to show the opposite, characterizing Mexico as a bastion of crime, illegal immigration and unfair trade.

Mr. Peña Nieto has faced a dilemma: to defend Mexico’s honor, or to defend its national interests by preserving ties with the United States at all costs.

For months, Mr. Peña Nieto made his choice clear. To the growing anger of many Mexicans, he avoided responding rashly to Mr. Trump. Calls for the building of a wall, promises to deport millions and threats to tear up the North American Free Trade Agreement have been met with measured, understated responses. Adding to his vulnerability are the millions of Mexican citizens living in the United States, whom Mr. Trump appeared to target in his executive orders on Wednesday.

For Mr. Peña Nieto, the economics were particularly difficult. Having begun his presidency with a focus on the economy, the idea of canceling Nafta or leaving Mexico a less desirable place for foreign investment was an existential crisis waiting to happen.

Dialogue, Mr. Peña Nieto said, was the only way forward. It was in keeping with the start of his administration, when he negotiated the passage of several major economic reforms with two rival political parties, paving the way for needed changes to the nation’s antiquated systems of telecommunications, energy and education.

Soon after that, his administration began to face headwinds. The disappearance of 43 teaching students, a scandal involving his wife’s purchase of a house, and a moribund economy began to gnaw at his popularity, and the slide in approval ratings continued from there.

By the time Mr. Peña Nieto invited Mr. Trump to Mexico for a visit during the American presidential campaign, his own image was as tarnished as the one Mr. Trump had painted of Mexico. The Mexican leader was trying to find common ground and engage in dialogue with the candidate, but at home, it was a political miscalculation. His reputation in Mexico sank even further.

But once Mr. Trump took office and pushed to make good on his campaign pledge to build a wall, the pressure on Mr. Peña Nieto became too great. Across the Mexican political and intellectual class, calls for him to cancel the meeting reached a fever pitch this week. Officials and experts said Mr. Trump’s Twitter post Thursday morning, suggesting he might cancel the meeting, made the decision less controversial: Mr. Peña Nieto could not let Mr. Trump be the one to cancel.

“It would have been like a cousin inviting us to dinner and then uninviting us, or worse, said we were only allowed to come if we paid for dinner,” Mr. Silva-Herzog said, referring to Mr. Trump’s repeated promises to make Mexico pay for the wall.

Now, despite the tensions with the United States and the problems they may cause, there is a silver lining, especially for the perception of Mr. Peña Nieto at home.

“These are ugly times, and things will get uglier. I don’t really see a way out it, but in this context, our great advantage will be that Mexicans are united,” Javier Solórzano, a prominent journalist, said in a video posted online. The country, he added, “is now united around the president.”